@@ -669,7 +669,7 @@ Reflect.ownKeys({ [Symbol()]:0, b:0, 10:0, 2:0, a:0 })
669669
670670## ` __proto__ ` 属性,Object.setPrototypeOf(),Object.getPrototypeOf()
671671
672- ** (1) ` __proto__ ` 属性**
672+ ### ` __proto__ ` 属性
673673
674674` __proto__ ` 属性(前后各两个下划线),用来读取或设置当前对象的` prototype ` 对象。目前,所有浏览器(包括 IE11)都部署了这个属性。
675675
@@ -723,9 +723,9 @@ Object.getPrototypeOf({ __proto__: null })
723723// null
724724```
725725
726- ** (2) Object.setPrototypeOf()**
726+ ### Object.setPrototypeOf()
727727
728- ` Object.setPrototypeOf ` 方法的作用与` __proto__ ` 相同,用来设置一个对象的` prototype ` 对象。它是ES6正式推荐的设置原型对象的方法 。
728+ ` Object.setPrototypeOf ` 方法的作用与` __proto__ ` 相同,用来设置一个对象的` prototype ` 对象,返回参数对象本身。它是 ES6 正式推荐的设置原型对象的方法 。
729729
730730``` javascript
731731// 格式
@@ -759,11 +759,29 @@ obj.y // 20
759759obj .z // 40
760760```
761761
762- 上面代码将proto对象设为obj对象的原型,所以从obj对象可以读取proto对象的属性 。
762+ 上面代码将 ` proto ` 对象设为 ` obj ` 对象的原型,所以从 ` obj ` 对象可以读取 ` proto ` 对象的属性 。
763763
764- ** (3)Object.getPrototypeOf() **
764+ 如果第一个参数不是对象,会自动转为对象。但是由于返回的还是第一个参数,所以这个操作不会产生任何效果。
765765
766- 该方法与setPrototypeOf方法配套,用于读取一个对象的prototype对象。
766+ ``` javascript
767+ Object .setPrototypeOf (1 , {}) === 1 // true
768+ Object .setPrototypeOf (' foo' , {}) === ' foo' // true
769+ Object .setPrototypeOf (true , {}) === true // true
770+ ```
771+
772+ 由于` undefined ` 和` null ` 无法转为对象,所以如果第一个参数是` undefined ` 或` null ` ,就会报错。
773+
774+ ``` javascript
775+ Object .setPrototypeOf (undefined , {})
776+ // TypeError: Object.setPrototypeOf called on null or undefined
777+
778+ Object .setPrototypeOf (null , {})
779+ // TypeError: Object.setPrototypeOf called on null or undefined
780+ ```
781+
782+ ### Object.getPrototypeOf()
783+
784+ 该方法与` Object.setPrototypeOf ` 方法配套,用于读取一个对象的原型对象。
767785
768786``` javascript
769787Object .getPrototypeOf (obj);
@@ -773,6 +791,7 @@ Object.getPrototypeOf(obj);
773791
774792``` javascript
775793function Rectangle () {
794+ // ...
776795}
777796
778797var rec = new Rectangle ();
@@ -785,6 +804,36 @@ Object.getPrototypeOf(rec) === Rectangle.prototype
785804// false
786805```
787806
807+ 如果参数不是对象,会被自动转为对象。
808+
809+ ``` javascript
810+ // 等同于 Object.getPrototypeOf(Number(1))
811+ Object .getPrototypeOf (1 )
812+ // Number {[[PrimitiveValue]]: 0}
813+
814+ // 等同于 Object.getPrototypeOf(String('foo'))
815+ Object .getPrototypeOf (' foo' )
816+ // String {length: 0, [[PrimitiveValue]]: ""}
817+
818+ // 等同于 Object.getPrototypeOf(Boolean(true))
819+ Object .getPrototypeOf (true )
820+ // Boolean {[[PrimitiveValue]]: false}
821+
822+ Object .getPrototypeOf (1 ) === Number .prototype // true
823+ Object .getPrototypeOf (' foo' ) === String .prototype // true
824+ Object .getPrototypeOf (true ) === Boolean .prototype // true
825+ ```
826+
827+ 如果参数是` undefined ` 或` null ` ,它们无法转为对象,所以会报错。
828+
829+ ``` javascript
830+ Object .getPrototypeOf (null )
831+ // TypeError: Cannot convert undefined or null to object
832+
833+ Object .getPrototypeOf (undefined )
834+ // TypeError: Cannot convert undefined or null to object
835+ ```
836+
788837## Object.keys(),Object.values(),Object.entries()
789838
790839### Object.keys()
@@ -1325,7 +1374,7 @@ const firstName = message?.body?.user?.firstName || 'default';
13251374
13261375` ` ` javascript
13271376// 如果 a 是 null 或 undefined, 返回 undefined
1328- // 否则返回 a? .b.c().d
1377+ // 否则返回 a.b.c().d
13291378a? .b .c ().d
13301379
13311380// 如果 a 是 null 或 undefined,下面的语句不产生任何效果
0 commit comments