JS中的prototypeproto与constructor

1. 寻找原型

心法口诀:每个对象的原型(__proto__)都指向自身的构造函数(constructor)的prototype属性

let b={}

b.constructor === Object
// true
b.__proto__ === Object.prototype
// true
b.__proto__ === b.constructor.prototype
// true
  1. 所以想要知道某个对象的原型是什么,首先找到他的构造函数是什么

9个终极类

Array.constructor
// ƒ Function() { [native code] }
Boolean.constructor
// ƒ Function() { [native code] }
Date.constructor
// ƒ Function() { [native code] }
Number.constructor
// ƒ Function() { [native code] }
String.constructor
// ƒ Function() { [native code] }
Object.constructor
// ƒ Function() { [native code] }
RegExp.constructor
// ƒ Function() { [native code] }
Symbol.constructor
// ƒ Function() { [native code] }

1个究极类

Function.constructor
// ƒ Function() { [native code] }

3中特殊数字对象

Math.constructor
// ƒ Object() { [native code] }
// Math 对象并不像 Date 和 String 那样是对象的类,因此没有构造函数 Math()
NaN.constructor
// ƒ Number() { [native code] }
Infinity.constructor
// ƒ Number() { [native code] }

2中bug类型

undefined.constructor
// VM25366:1 Uncaught TypeError: Cannot read property 'constructor' of undefined at <anonymous>:1:11
null.constructor
// VM25366:1 Uncaught TypeError: Cannot read property 'constructor' of null at <anonymous>:1:11

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理