31.对象方法的扩展

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      // 1.Object.is  判断两个值是否完全相等,类似于===,但是又不太一样,比如NaN===NaN
      // console.log(Object.is(120, 100));
      // console.log(Object.is(NaN, NaN));
      // console.log(NaN === NaN);
      // console.log(NaN !== NaN);

      // 2.Object.assign  对象的合并---也可用于对象的浅拷贝
      let config1 = {
        host: "localhost",
        port: 3306,
        name: "root",
        password: "root",
        test1: "测试1",
      };

      let config2 = {
        host: "https://www.baidu.com",
        port: 33060,
        name: "百度",
        password: 123,
        test2: "测试2",
      };
      console.log(Object.assign(config1, config2)); //后面对象里的属性会把前面对象里同名的属性覆盖掉,不相同的则保留

      // 3.Object.setPrototypeOf,Object.getPrototypeOf
      let school = {
        name: "xxx",
      };

      let cities = {
        address: ["xx1", "xx2", "xx3"],
      };

      // 此方法不建议这样做,应该在创建对象的时候就把原型设置好,效率高     Object.create(proto, [propertiesObject])
      Object.setPrototypeOf(school, cities); //为school设置原型对象为cities
      console.log(school);
      console.log(Object.getPrototypeOf(school));
    </script>
  </body>
</html>
posted @ 2022-01-02 16:29  问某完红  阅读(20)  评论(0编辑  收藏  举报