@@ -669,7 +669,7 @@ Reflect.ownKeys({ [Symbol()]:0, b:0, 10:0, 2:0, a:0 })
669
669
670
670
## ` __proto__ ` 属性,Object.setPrototypeOf(),Object.getPrototypeOf()
671
671
672
- ** (1) ` __proto__ ` 属性**
672
+ ### ` __proto__ ` 属性
673
673
674
674
` __proto__ ` 属性(前后各两个下划线),用来读取或设置当前对象的` prototype ` 对象。目前,所有浏览器(包括 IE11)都部署了这个属性。
675
675
@@ -723,9 +723,9 @@ Object.getPrototypeOf({ __proto__: null })
723
723
// null
724
724
```
725
725
726
- ** (2) Object.setPrototypeOf()**
726
+ ### Object.setPrototypeOf()
727
727
728
- ` Object.setPrototypeOf ` 方法的作用与` __proto__ ` 相同,用来设置一个对象的` prototype ` 对象。它是ES6正式推荐的设置原型对象的方法 。
728
+ ` Object.setPrototypeOf ` 方法的作用与` __proto__ ` 相同,用来设置一个对象的` prototype ` 对象,返回参数对象本身。它是 ES6 正式推荐的设置原型对象的方法 。
729
729
730
730
``` javascript
731
731
// 格式
@@ -759,11 +759,29 @@ obj.y // 20
759
759
obj .z // 40
760
760
```
761
761
762
- 上面代码将proto对象设为obj对象的原型,所以从obj对象可以读取proto对象的属性 。
762
+ 上面代码将 ` proto ` 对象设为 ` obj ` 对象的原型,所以从 ` obj ` 对象可以读取 ` proto ` 对象的属性 。
763
763
764
- ** (3)Object.getPrototypeOf() **
764
+ 如果第一个参数不是对象,会自动转为对象。但是由于返回的还是第一个参数,所以这个操作不会产生任何效果。
765
765
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 ` 方法配套,用于读取一个对象的原型对象。
767
785
768
786
``` javascript
769
787
Object .getPrototypeOf (obj);
@@ -773,6 +791,7 @@ Object.getPrototypeOf(obj);
773
791
774
792
``` javascript
775
793
function Rectangle () {
794
+ // ...
776
795
}
777
796
778
797
var rec = new Rectangle ();
@@ -785,6 +804,36 @@ Object.getPrototypeOf(rec) === Rectangle.prototype
785
804
// false
786
805
```
787
806
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
+
788
837
## Object.keys(),Object.values(),Object.entries()
789
838
790
839
### Object.keys()
@@ -1325,7 +1374,7 @@ const firstName = message?.body?.user?.firstName || 'default';
1325
1374
1326
1375
` ` ` javascript
1327
1376
// 如果 a 是 null 或 undefined, 返回 undefined
1328
- // 否则返回 a? .b.c().d
1377
+ // 否则返回 a.b.c().d
1329
1378
a? .b .c ().d
1330
1379
1331
1380
// 如果 a 是 null 或 undefined,下面的语句不产生任何效果
0 commit comments