【问题标题】:How to use object oriented without using keyword ('new')如何在不使用关键字('new')的情况下使用面向对象
【发布时间】:2016-03-13 17:06:06
【问题描述】:

我想要做的如下:

var Person = function(name) {
   this.name = name;
}
Person.prototype.getName = function () {
   return this.name;
}

// This will return error;
console.log(Person('John').getName());
// While this won't.
var p1 = new Person('john');
console.log(p1.getName());

我是不是误会了什么?

【问题讨论】:

  • 当您使用new时,会创建一个实例,并且原型中定义的函数可用于对象
  • 你为什么要避免使用new
  • @mattBrowne 就是这样,我正在尝试摆脱 new 而不返回任何错误。我只是在尝试为新项目创建库时展示了一个小示例。

标签: javascript oop


【解决方案1】:
// This will return error;
console.log(Person('John').getName());

它返回一个错误bcoz Person() 默认返回undefined,但如果你使用new 它会返回新创建的对象。

// While this won't.
var p1 = new Person('john');
console.log(p1.getName());

这很有效,因为返回了一个将__proto__ 设置为Person.prototype 的新对象,并且由于上面有一个getName(),所以它可以按预期工作。

您可以使用作用域安全构造函数让您的构造函数在没有显式 new 的情况下工作。

function Person(name) {
  if(this instanceof Person) {
    this.name = name;
  } else {
    return new Person(name);
  }
}

http://www.mikepackdev.com/blog_posts/9-new-scope-safe-constructors-in-oo-javascript

【讨论】:

    【解决方案2】:

    如果你不想在你的代码中使用 new 关键字(我想不出一个很好的理由,你基本上会隐藏一个重要信息),你可以做点什么喜欢:

    var pPerson = function(name) {
       this.name = name;
    };
    
    pPerson.prototype.getName = function () {
       return this.name;
    };
    
    var Person = function (name) {
        return new pPerson(name);
    };
    

    【讨论】:

      【解决方案3】:

      如果您不想使用new 关键字,可以使用Object.create()。这是来自 MDN 的示例:

      // Animal properties and method encapsulation
      var Animal = {
        type: "Invertebrates", // Default value of properties
        displayType : function(){  // Method which will display type of Animal
          console.log(this.type);
        }
      }
      
      // Create new animal type called animal1 
      var animal1 = Object.create(Animal);
      animal1.displayType(); // Output:Invertebrates
      
      // Create new animal type called Fishes
      var fish = Object.create(Animal);
      fish.type = "Fishes";
      fish.displayType(); // Output:Fishes
      

      【讨论】:

      • 我不想创建2个函数,应该有办法摆脱“new”关键字。
      • @AhmedAlaa 两个函数?
      • 哇,很抱歉错过了这里。
      【解决方案4】:

      如果你真的很讨厌自己,你可以这样做

      var Person = function(name) {
         var This = {}; 
      
         This.name = name;
      
         //See note
         Object.setPrototypeOf(This, arguments.callee.prototype);  
      
         return This;
      }
      Person.prototype.getName = function () {
         return this.name;
      }
      
      var p = Person('John');
      console.log(p.getName());
      

      注意 你绝对必须阅读this

      【讨论】:

        【解决方案5】:

        您可以尝试创建原型函数作为父函数本身的一部分。

        var Person = function(name) {
          this.name = name;
          this.get_name = function() {
            return this.name;
          }
          return this;
        }
        
        Person.prototype.getName = function() {
          return this.name;
        }
        
        // This will return error;
        console.log(Person('John').get_name());
        
        // While this won't.
        var p1 = new Person('john');
        console.log(p1.getName());

        【讨论】:

          猜你喜欢
          • 2010-12-25
          • 1970-01-01
          • 1970-01-01
          • 2010-12-07
          • 1970-01-01
          • 2018-02-25
          • 2017-06-21
          相关资源
          最近更新 更多