我们说我有以下简单的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"
这两种实现之间的区别是什么?
答案 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>
但对于所有常用功能,我建议使用原型。