博客
关于我
18.对象方法的扩展
阅读量:398 次
发布时间:2019-03-05

本文共 824 字,大约阅读时间需要 2 分钟。

对象方法扩展

以下是关于JavaScript对象方法扩展的实践示例:
  • 对象比较方法
    Object.is()方法提供了一个简单的方法来判断两个值是否完全相等。
    • console.log(Object.is(120, 120));
    • console.log(Object.is(NaN, NaN));
    • console.log(NaN === NaN);
      值得注意的是, NaN与自身比较永远返回false。
    1. 对象合并
      Object.assign()方法用于合并两个对象的属性,第一参数是目标对象,后续参数是要合并的对象。
    2. const config1 = { host: 'localhost', port: 3306, name: 'root', pass: 'root', test: 'test' };const config2 = { host: 'http://atguigu.com', port: 33060, name: 'atguigu.com', pass: 'iloveyou', test2: 'test2' };console.log(Object.assign(config1, config2));
      1. 原型对象设置
        Object.setPrototypeOf()方法用于设置对象的原型。
      2. const school = { name: 'study' };const cities = { xiaoqu: ['北京', '上海', '深圳'] };Object.setPrototypeOf(school, cities);console.log(Object.getPrototypeOf(school)); // 输出: { xiaoqu: ['北京', '上海', '深圳'] }console.log(school); // 输出: { name: 'study', xiaoqu: ['北京', '上海', '深圳'] }

    转载地址:http://mmazz.baihongyu.com/

    你可能感兴趣的文章
    Python 中生成器与普通函数的区别
    查看>>
    Python 中的 *tuple 和 **dict 是什么意思?
    查看>>
    Python 中的 filter() 函数:筛选可迭代对象元素
    查看>>
    Python 中的 Pillow 不允许我打开图像(“超出限制“)
    查看>>
    Python 中的 threading 模块和 multiprocessing 模块有何区别?
    查看>>
    Python 中的 threading 模块和 multiprocessing 模块有何区别?
    查看>>
    Python 中的 Turtle 库详解
    查看>>
    Python 中的 __slots__ 属性有什么作用?
    查看>>
    Python 中的... 三点、箭头(->)、/ 分别的作用
    查看>>
    python读取json数据的id_python处理JSON数据
    查看>>
    Python 中的Duck Typing
    查看>>
    Python 中的for in 遍历生成字典、添加判断条件if
    查看>>
    Python 中的Lock与RLock
    查看>>
    Python 中的range(),arange()函数
    查看>>
    Python 中的内存管理机制
    查看>>
    Python 中的函数包装器:模型运行时和调试
    查看>>
    Python 中的列表理解和 lambda
    查看>>
    Python 中的多层for in嵌套循环使用
    查看>>
    Python 中的多线程(01/4)
    查看>>
    Python 中的多进程(01/2):简介
    查看>>