如果我创建一个简单的对象
var a = {foo: 1};
为什么我可以通过这种方式访问其原型?
a.prototype
我在控制台上未定义
答案 0 :(得分:0)
您可以通过以下方式找到对象的原型:
a.__proto__
obj.prototype
可用于为对象设置新的原型。
答案 1 :(得分:0)
函数具有prototype
属性,而不是常规对象。 prototype
属性指向使用new
创建的子对象将链接到的对象。
对象是原型已链接。但是,这与
函数的prototype
属性这种命名有点令人困惑。
例如:
var a = {foo: 1};
// the prototype of a
console.log(Object.getPrototypeOf(a))
// the same as __proto__
console.log(a.__proto__ === Object.getPrototypeOf(a))
// the .protoype property:
function Test(){}
// it's just an empty object unless you change it
console.log(Test.prototype)
// instance are prototype linked to this
let instance = new Test()
console.log(instance.__proto__ === Test.prototype)