OOP JS:在基础对象中使用prototype属性是否有意义?

时间:2015-07-25 16:42:34

标签: javascript oop

我们说我有以下简单的OOP实现:

实施1:

function Base() {
    this.text = "lol";
}

Base.prototype.hello = function() {
    console.log("Hello from base");
}

function Child() {
}

Child.prototype = new Base(); // All objects created from Child function now have reference to this Base object in their __proto__ property.

Child.prototype.hello = function() { // Changes the hello implementation in the prototype object
    console.log("Hello from child");
}

var child1 = new Child(); 
child1.hello(); // "Hello from child"

这是我经常看到的实现:使用prototype属性定义从Base对象到子对象应该继承的方法(比如hello方法)。

但是,如果我使用此关键字将hello方法绑定到基础对象,我得到相同的结果:

实施2:

function Base() {
    this.text = "lol";
    this.hello = function() {
        console.log("Hello from base");
    }
}

function Child() {
}

Child.prototype = new Base(); // All objects created from Child function now have reference to this Base object in their __proto__ property.

Child.prototype.hello = function() { // Changes the hello implementation in the prototype object
    console.log("Hello from child");
}

var child1 = new Child();
child1.hello(); // "Hello from child"

这两种实现之间的区别是什么?

2 个答案:

答案 0 :(得分:3)

原型方法由同一个类下的所有对象共享,而向对象本身添加方法使该函数对该特定对象是私有的。

如果您只从该类创建一个对象,则性能没有差异。但是,如果要创建多个对象,原型方法将显着提高性能。

这是一个例子

function Base() {
    this.text = "lol";
}

Base.prototype.hello = function() {
    console.log("Hello from base");
}

var obj1 = new Base();
var obj2 = new Base();
obj1.hello();
obj2.hello();

Base.prototype.hello = function() {
    console.log("Bye");
}

obj1.hello();
obj2.hello();

function Base() {
    this.text = "lol";
    this.hello = function() {
        console.log("Hello from base");
    };
}

var obj1 = new Base();
var obj2 = new Base();
obj1.hello();
obj2.hello();

obj1.hello = function() {
    console.log("Bye");
}

obj1.hello();
obj2.hello();

答案 1 :(得分:2)

基本上你在问:

之间有什么区别
function Base() {
    this.text = "lol";
}

Base.prototype.hello = function() {
    console.log("Hello from base");
}

function Base() {
    this.text = "lol";
    this.hello = function() {
        console.log("Hello from base");
    }
}

从功能上讲,没有。

但是:在后一种设置中,每个Base实例都包含 hello方法的单独副本

这意味着

  • 两个对象实例的物理尺寸将大于前一个设置的两个对象实例。
  • 使用以前的设置,您可以在一个步骤中覆盖所有实时实例的方法(想象一下调试大型对象图):您只需在原型上交换它。
  • 使用后一种设置,您必须单独更换每个实时实例上的方法。
  • 前一个设置中的方法无法访问实例变量(在构造函数中使用var声明的变量)。
  • 后一种设置中的方法可以。
  • 原型设置中的方法本质上是公开的。

我个人认为原型设置更清洁:它将功能与对象实例分开。它节省了内存。它迫使你使用合理的范围。

如果您需要一个函数来访问私有实例数据(在任何情况下都不想分配给this),那么在构造函数中添加该方法是唯一的方法。< / p>

但对于所有常用功能,我建议使用原型。