JavaScript创建原型对象并将其分配给另一个对象

JavaScript创建原型对象并将其分配给另一个对象,javascript,inheritance,prototype,prototypal-inheritance,Javascript,Inheritance,Prototype,Prototypal Inheritance,我有: 结果: 为什么??我做错了什么?对于像函数那样工作的对象,没有像prototype这样的特殊属性。你想要的只是Object.create(x): // prototype object var x = { name: "I am x" }; // object that will get properties of prototype object var y = {}; // assign the prototype of y from x y.prototype = O

我有:

结果:


为什么??我做错了什么?

对于像函数那样工作的对象,没有像
prototype
这样的特殊属性。你想要的只是
Object.create(x)

// prototype object
var x = {
    name: "I am x"
};

// object that will get properties of prototype object
var y = {};

// assign the prototype of y from x
y.prototype = Object.create( x );

// check
console.log( y.__proto__ );

对于行为类似于函数的对象,没有类似于
prototype
的特殊属性。你想要的只是
Object.create(x)

// prototype object
var x = {
    name: "I am x"
};

// object that will get properties of prototype object
var y = {};

// assign the prototype of y from x
y.prototype = Object.create( x );

// check
console.log( y.__proto__ );

prototype
属性仅适用于函数对象。否则它只是另一个属性。
prototype
属性将仅适用于函数对象。否则,它只是另一种财产。