- 原型
- 原型图解
- prototype 详解
- proto 详解
constructor__proto__Object.create(parent, propertyDescriptor)**Object.setPrototypeOf(obj, parent)**Object.getPrototypeOf(obj)**Object.is(value, value)Object.assign()Object.keys()Object.values()Object.entries()Object.fromEntries()Object.getOwnPropertyNames()Object.getOwnPropertySymbols()Object.getOwnPropertyDescriptor()Object.getOwnPropertyDescriptors()Object.defineProperties()Object.defineProperty()Object.preventExtensions()Object.freeze()Object.seal()Object.isExtensible()Object.isFrozen()Object.isSealed()
- 继承
原型
prototype
显示原型 / 原型对象
只有 Object 和函数对象中存在*原型对象
* 原型对象是公有的,所有继承的对象都可以访问其中的属性和方法**
构造函数通过*原型对象中的 constructor 方法构造对象实例,对象实例会继承构造方法中的原型对象
* 构造函数的原型对象的原型是继承的**原型对象
Object 的*原型对象的原型**是 null
**
__proto__
隐式原型 / 原型
所有对象中都存在*原型
* 原型中的属性和方法是对象私有的
* 对象的原型的原型继承自其父级的**原型对象
Object 的*原型的原型是 Object 的原型对象**
原型图解
prototype 详解
constructor
* 构造函数的原型对象中的构造方法指向构造函数
* Object 的原型对象中的构造方法指向 Object
**
__proto__
*继承的**原型对象
parent.isPrototypeOf(obj)**
**_desc_**检测是否是原型对象**params**{ object } parent 检测的**原型对象(父级对象,不能直接设置 prototype**)**params**{ object } obj 检测的对象**return**{ boolean }
var parent = {};var obj = {};var bool1 = parent.isPrototypeOf(obj);console.log(bool1); // falseObject.setPrototypeOf(obj, parent);var bool2 = parent.isPrototypeOf(obj);console.log(bool2); // true
obj.toString()**
**_desc_**转换为字符串**params**{ object } obj**return**{ string } 除非重写,否则对象返回 [object Object] 字符串
var num = 100;var bol = false;var obj = {};console.log(num.toString()); // "100"console.log(bol.toString()); // "false"console.log(obj.toString()); // "[object Object]"
obj.valueOf()
* 主要用于获取通过对象形式声明的变量的值
* 通过对象形式声明的引入类型,使用 valueOf 无变化
*通过对象形式声明的基本类型,使用 valueOf 会获取字面量形式声明的值**
**_desc_**获取对象原始值**params**{ object } obj**return**{ any }
var num = new Number(100);var str = new String("Hello World");var obj = new Object();console.log(num); // Number {100}console.log(str); // String {"Hello World"}console.log(obj); // {}console.log(num.valueOf()); // 100console.log(str.valueOf()); // "Hello World"console.log(obj.valueOf()); // {}
obj.toLocaleString()**
**_desc_**转换为本地格式的字符串**params**{ object } obj**return**{ string } 除非重写,否则对象返回 [object Object] 字符串
var num = 100;var bol = false;var obj = {};console.log(num.toLocaleString()); // "100"console.log(bol.toLocaleString()); // "false"console.log(obj.toLocaleString()); // "[object Object]"
obj.hasOwnProperty()**
obj.propertyIsEnumerable()
* 上述方法详见 引用类型 - 对象 Object - 类型检测
**
proto 详解
constructor
__proto__
*继承的**原型对象
Object.create(parent, propertyDescriptor)**
**_desc_**创建对象并指定**原型对象 _`params`_ { object } parent 设置的原型对象(父级对象,不能直接设置 prototype) _`params`_ { object } propertyDescriptor 属性特征描述 _`return`_ { object } 新的对象**
function Human() {this.run = function() {console.log("run");}}var obj = Object.create(new Human(), {name: {value: "zhangsan",writable: true,enumerable: true,configurable: true},age: {value: 18,writable: true,enumerable: true,configurable: true}});console.log(obj); // { name: "zhangsan", age: 18 }obj.run(); // "run"
Object.setPrototypeOf(obj, parent)**
**_desc_**设置**原型对象**(继承)**params**{ object } obj**params**{ object } parent 设置的**原型对象(父级对象,不能直接设置 prototype**)**return**{ object } 原对象
function Human() {this.run = function() {console.log("prototype - run");}}Human.eat = function() {console.log("__proto__ - eat");}var obj = {};var _obj = Object.setPrototypeOf(obj, new Human());console.log(_obj === obj); // trueobj.run(); // "prototype - run"obj.eat(); // error: obj.eat is not a function
Object.getPrototypeOf(obj)**
**_desc_**获取**原型对象 _`params`_ { object } obj _`return`_ { object } **原型对象 prototype
function Human() {}var human = new Human();var proto = Object.getPrototypeOf(human);console.log(proto); // { constructor: Human() }console.log(proto === Human.prototype); // true
Object.is(value, value)
* 与 === 运算符大体相同,但存在区别:
* === 运算符将 -0 和 +0 视为相等,但 Object.is() 视为不相等
=== 运算符将 NaN 和 NaN 视为不相等,但* Object.is() 视为相等
* === 运算符将 Number.NaN 和 NaN 视为不相等,但** Object.is() 视为相等
**_desc_**判断两个值是否相同**params**{ any } value 比较的值**return**{ boolean }
console.log(-0 === +0); // trueconsole.log(NaN === NaN); // falseconsole.log(Number.NaN === NaN); // falseconsole.log(Object.is(-0, +0)); // falseconsole.log(Object.is(NaN, NaN)); // trueconsole.log(Object.is(Number.NaN, NaN)); // true
Object.assign()
* 上述方法详见 引用类型 - 对象 Object - 类型管理
**
Object.keys()
Object.values()
Object.entries()
Object.fromEntries()
Object.getOwnPropertyNames()
Object.getOwnPropertySymbols()
* 上述方法详见 引用类型 - 对象 Object - 枚举遍历
Object.getOwnPropertyDescriptor()
Object.getOwnPropertyDescriptors()
Object.defineProperties()
Object.defineProperty()
Object.preventExtensions()
Object.freeze()
Object.seal()
* 上述方法详见 引用类型 - 对象 Object - 属性特征
**
Object.isExtensible()
Object.isFrozen()
Object.isSealed()
* 上述方法详见 引用类型 - 对象 Object - 属性检测
