newPromise / js-

0 stars 0 forks source link

Object.getPrototypeof( ) 方法 #21

Open newPromise opened 6 years ago

newPromise commented 6 years ago

使用 Object.getPrototypeof(obj) 方法返回的是一个原型 obj 这是要返回原型的对象 return value 返回的是 obj所属类型 中的 [[prototype]] 特性 或者是对象的 _proto_ 属性

解释

例如:

let  str = "string";
Object.getPrototypeof( str ) === String.prototype  // true

因为这里: str 属于字符串类型, 使用 Object.getPrototypeof 得到的是 String.prototype

newPromise commented 6 years ago

使用 Object.getPrototypeof( )的几种方法

let arr = [];
let obj = {};
let str = '';

Object.getPrototypeof( obj ) === Object.prototype;
// true
Object.getPrototypeof( arr ) === Array.prototype;
// true
Object.getPrototypeof( str ) === String.prototype;
 // true
newPromise commented 6 years ago

也就是说,使用 Object.getPrototypeof 得到的是构造函数的原型

function bar () {
}
let foo = new bar();
Object.getPrototypeof( foo ) === bar.prototype
// true

以为上面的代码中实际上实现的使用用原型链的继承,使用 new 操作符构造的实例,实例上的 prototype 指向了构造函数的原型

这实际上表示了继承关系