JS中的prototype、__proto__與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

...函數

相關文章
相關標籤/搜索