(三十六)JavaScript原型链
1:基本概念
/* 定义数字类型 */
const num = 1234;
/* 包含toString()、toFixed()等方法 */
num.toFixed(2);
/* 查看num的构造函数 */
num.__proto__
/*
Number {
toPrecision: ƒ, …}
constructor: ƒ Number()
toExponential: ƒ toExponential()
toFixed: ƒ toFixed()
toLocaleString: ƒ toLocaleString()
toPrecision: ƒ toPrecision()
toString: ƒ toString()
valueOf: ƒ valueOf()
__proto__: Object
}
*/
/* Number的构造函数 */
num.__proto__.__proto__
/*
{
constructor: ƒ doSomething(),
__proto__: {
constructor: ƒ Object(),
hasOwnProperty: ƒ hasOwnProperty(),
isPrototypeOf: ƒ isPrototypeOf(),
propertyIsEnumerable: ƒ propertyIsEnumerable(),
toLocaleString: ƒ toLocaleString(),
toString: ƒ toString(),
valueOf: ƒ valueOf()
}
}
*/
/* 继续查找 */
num.__proto__.__proto__.__proto__
/* null */
-
prototype - 原型
函数特有属性
指向原型对象的指针
包含所有实例共享的属性和方法
function fun() {}
fun.prototype
/*
{
constructor: ƒ fun(),
__proto__: {
constructor: ƒ Object(),
hasOwnProperty: ƒ hasOwnProperty(),
isPrototypeOf: ƒ isPrototypeOf(),
propertyIsEnumerable: ƒ propertyIsEnumerable(),
toLocaleString: ƒ toLocaleString(),
toString: ƒ toString(),
valueOf: ƒ valueOf()
}
}
*/
Object.getPrototypeOf(new Fun()) === Fun.prototype