Object.create(prototype) 和 Object(prototype) 的区别
Difference between Object.create(prototype) and Object(prototype)
一个快速但难以google的问题:
var child = Object.create(parent.prototype);
var child = Object(parent.prototype);
它们相同吗?
编辑:
这两个用于实现寄生组合继承模式的 inheritPrototype 函数示例提出了我的问题。
function inheritPrototype(childObject, parentObject) {
var copyOfParent = Object.create(parentObject.prototype);
copyOfParent.constructor = childObject;
childObject.prototype = copyOfParent;
}
http://javascriptissexy.com/oop-in-javascript-what-you-need-to-know/
function inheritPrototype(subType, superType){
var prototype = object(superType.prototype);
prototype.constructor = subType;
subType.prototype = prototype;
}
"Parasitic Combination Inheritance" in Professional JavaScript for Web Developers
不,它们并不相同,见下文:
Object.create(prototype)
将创建一个新的 原型 ,它 从 prototype
继承了 作为参数
Object(obj)
或 new Object(obj)
将创建一个新的 实例 obj
并调用构造函数。
现在由于原型也是 javascript 对象,所以差异可以很小。但是 Object.create
可以处理可能需要参数的构造函数,而 new Object()
不会
this SO related answer 中的更多信息(例如差异继承、自定义属性等)
参考文献:
更新 OP 编辑后
在我的 OOP javascript 框架 Classy.js 周围有这个 polyfill Object.create
适用于较旧的浏览器,这可能会阐明您在差异之间的进一步问题:
Create = Obj.create || function( proto, properties ) {
var Type = function () {}, TypeObject;
Type[PROTO] = proto;
TypeObject = new Type( );
TypeObject[__PROTO__] = proto;
if ( 'object' === typeOf(properties) ) defineProperties(TypeObject, properties);
return TypeObject;
}
这个 polyfill 可以像 Object.create
一样处理任意构造函数(带参数),并且还确保 __proto__
被正确分配。使用 Object(prototype)
的模式是试图(弱)引用 prototype
.
不,它们不一样:
Object(X) or new Object(X) are same as Object.create(X.prototype)
Object(X)
将创建对象和 运行 Constructor
(即新创建的对象继承自构造函数的原型)。 Object.create(X.prototype)
正在使用另外 运行ning Constructor
.
创建对象
Object(X) or new Object(X) are not as Object.create()
Object.create()
正在创建没有 运行ning 的对象 Constructor
(即,它将创建一个不继承任何对象的对象)
新对象将子项设置为父项:
var parent = {name : 'proto'};
var child = new Object(parent);
console.log(child === parent);//=true
//if I set child.name it will change parent.name
对象创建returns一个对象实例,它使用第一个参数作为原型(原型链中使用的第一个原型)。
child = Object.create(parent);
console.log(child === parent);//=false
//I can set child.name because it will be shadowed
// and will not change parent.name
console.log(child.hasOwnProperty('name'));//=false
可在此处找到有关原型的更多信息:
一个快速但难以google的问题:
var child = Object.create(parent.prototype);
var child = Object(parent.prototype);
它们相同吗?
编辑:
这两个用于实现寄生组合继承模式的 inheritPrototype 函数示例提出了我的问题。
function inheritPrototype(childObject, parentObject) {
var copyOfParent = Object.create(parentObject.prototype);
copyOfParent.constructor = childObject;
childObject.prototype = copyOfParent;
}
http://javascriptissexy.com/oop-in-javascript-what-you-need-to-know/
function inheritPrototype(subType, superType){
var prototype = object(superType.prototype);
prototype.constructor = subType;
subType.prototype = prototype;
}
"Parasitic Combination Inheritance" in Professional JavaScript for Web Developers
不,它们并不相同,见下文:
Object.create(prototype)
将创建一个新的 原型 ,它 从prototype
继承了 作为参数Object(obj)
或new Object(obj)
将创建一个新的 实例obj
并调用构造函数。
现在由于原型也是 javascript 对象,所以差异可以很小。但是 Object.create
可以处理可能需要参数的构造函数,而 new Object()
不会
this SO related answer 中的更多信息(例如差异继承、自定义属性等)
参考文献:
更新 OP 编辑后
在我的 OOP javascript 框架 Classy.js 周围有这个 polyfill Object.create
适用于较旧的浏览器,这可能会阐明您在差异之间的进一步问题:
Create = Obj.create || function( proto, properties ) {
var Type = function () {}, TypeObject;
Type[PROTO] = proto;
TypeObject = new Type( );
TypeObject[__PROTO__] = proto;
if ( 'object' === typeOf(properties) ) defineProperties(TypeObject, properties);
return TypeObject;
}
这个 polyfill 可以像 Object.create
一样处理任意构造函数(带参数),并且还确保 __proto__
被正确分配。使用 Object(prototype)
的模式是试图(弱)引用 prototype
.
不,它们不一样:
Object(X) or new Object(X) are same as Object.create(X.prototype)
Object(X)
将创建对象和 运行 Constructor
(即新创建的对象继承自构造函数的原型)。 Object.create(X.prototype)
正在使用另外 运行ning Constructor
.
Object(X) or new Object(X) are not as Object.create()
Object.create()
正在创建没有 运行ning 的对象 Constructor
(即,它将创建一个不继承任何对象的对象)
新对象将子项设置为父项:
var parent = {name : 'proto'};
var child = new Object(parent);
console.log(child === parent);//=true
//if I set child.name it will change parent.name
对象创建returns一个对象实例,它使用第一个参数作为原型(原型链中使用的第一个原型)。
child = Object.create(parent);
console.log(child === parent);//=false
//I can set child.name because it will be shadowed
// and will not change parent.name
console.log(child.hasOwnProperty('name'));//=false
可在此处找到有关原型的更多信息: