Browse Source

fix:day23

e 1 year ago
parent
commit
d7bde825f7
2 changed files with 70 additions and 0 deletions
  1. 11 0
      day23/html/2.对象的扩展方法.html
  2. 59 0
      day23/js/2.对象的扩展方法.js

+ 11 - 0
day23/html/2.对象的扩展方法.html

@@ -0,0 +1,11 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <meta name="viewport" content="width=device-width, initial-scale=1.0">
+    <title>Document</title>
+</head>
+<body>
+    <script src="../js/2.对象的扩展方法.js"></script>
+</body>
+</html>

+ 59 - 0
day23/js/2.对象的扩展方法.js

@@ -0,0 +1,59 @@
+// 1.Object.is() 比较两个值是否严格相等(===)
+console.log(Object.is(120,120))
+console.log(Object.is(NaN,NaN))
+
+//2.Object.assign() 对象合并:后面的对象会将前一个相同属性覆盖,其余合并
+let obj1 = {
+    name:"Lucy",
+    age:18,
+    sex: "女"
+};
+let obj2 = {
+    names:'LiLi',
+    ages: 28
+}
+console.log(Object.assign(obj1,obj2));
+
+// 3.Object.setPrototypeOf 设置原型对象
+// 4.Object.getPrototypeOf 获取原型对象
+const school = {
+    name:'MySchool'
+}
+const city = {
+    area:['北京','上海','哈尔滨']
+}
+Object.setPrototypeOf(school,city);
+console.log(Object.getPrototypeOf(school),'school')
+console.log(school)
+
+// es8对象扩展方法
+// 5.Object.keys() 返回给定对象所有可枚举的键值数组
+var person = {
+    name:'我的名字',
+    age: 33
+}
+console.log(Object.keys(person),'keys')
+
+// 6.Object.values() 返回给定对象所有可枚举的属性值数组
+console.log(Object.values(person),'values')
+
+// 7.Object.entries() 返回给定对象自身可遍历的[key,value]的数组
+console.log(Object.entries(person),'entires')
+
+// 8.Object.getOwnPropertyDescriptors() 返回指定对象所有自身属性的描述对象
+// 9.Object.create({},{})
+console.log(Object.getOwnPropertyDescriptors(person));
+const obj = Object.create(null,{
+    name: {
+        value: '阿珍',
+        writable: true
+    },
+    age: {
+        value: '40',
+        writable: false,
+        configurable:false,
+        enumerable: false
+    }
+})
+
+console.log(Object.getOwnPropertyDescriptors(obj));