zoukankan      html  css  js  c++  java
  • Object的常用方法

    一.常用Object方法:

    Object.keys()

    遍历可枚举的属性,只包含对象本身可枚举属性,不包含原型链可枚举属性

    let obj ={
        a:1,
        b:2,
        c:3,
    }
    Object.keys(obj);
    // [ 'a', 'b', 'c' ]

    let str = "saasd字符串"

    Object.keys(str) // ["0", "1", "2", "3", "4", "5", "6", "7"]

    let arr = [1,2,3,4,5,6]

    Object.keys(arr) // ["0", "1", "2", "3", "4", "5"]

    Object.values()

    遍历可枚举的属性值,只包含对象本身可枚举属性值,不包含原型链可枚举属性值

    let obj ={
        a:1,
        b:2,
        c:3,
    }
    Object.values(obj);
    // [ 1, 2, 3 ]

    Object.entries()

    分割对象

    const obj = { foo: 'bar', baz: 42 };
    console.log(Object.entries(obj)); // [ ['foo', 'bar'], ['baz', 42] ]
    
    // array like object
    const obj = { 0: 'a', 1: 'b', 2: 'c' };
    console.log(Object.entries(obj)); // [ ['0', 'a'], ['1', 'b'], ['2', 'c'] ]
    
    //  string
    Object.entries('abc')   // [['0', 'a'], ['1', 'b'], ['2', 'c']]
    
    Object.entries(100)   // []
    Object.assign(target,source,source1)

    用于对象的合并,将源对象(source)的所有可枚举属性,复制到目标对象(target)。拷贝的属性是有限制的,只拷贝源对象的自身属性(不拷贝继承属性),也不拷贝不可枚举的属性(enumerable: false)

    const target = { a: 1, b: 1 };
    
    const source1 = { b: 2, c: 2 };
    const source2 = { c: 3 };
    
    Object.assign(target, source1, source2); target // {a:1, b:2, c:3}
    
    特殊情况:
    let obj = {a: 1};
    Object.assign(obj, undefined) === obj  // true
    Object.assign(obj, null) === obj       // true
    
    Object.assign([1, 2, 3], [4, 5])        // [4, 5, 3]
    
    Object.assign方法实行的是浅拷贝,而不是深拷贝。
    
    const obj1 = {a: {b: 1}};
    const obj2 = Object.assign({}, obj1);
    
    obj1.a.b = 2;
    console.log(obj2.a.b) //2
    obj2.a.b = 3
    console.log(obj1.a.b) //3

    Object.is()

    它用来比较两个值是否严格相等,与严格比较运算符(===)的行为基本一致

    Object.is('foo', 'foo')     // true
    
    Object.is({}, {})           // false
    
    不同于 === 之处
    +0 === -0                   //true
    NaN === NaN                     // false
    
    Object.is(+0, -0)           // false
    Object.is(NaN, NaN)         // true

     Object.hasOwnProperty()

    hasOwnProperty表示是否有自己的属性。这个方法会查找一个对象是否有某个属性,但是不会去查找它的原型链(继承属性)。

    var obj = {
        a: 1,
        fn: function(){
     
        },
        c:{
            d: 5
        }
    };
    console.log(obj.hasOwnProperty('a'));  // true
    console.log(obj.hasOwnProperty('fn'));  // true
    console.log(obj.hasOwnProperty('c'));  // true
    console.log(obj.c.hasOwnProperty('d'));  // true
    console.log(obj.hasOwnProperty('d'));  // false, obj对象没有d属性
     
    var str = new String();
    // split方法是String这个对象的方法,str对象本身是没有这个split这个属性的
    console.log(str.hasOwnProperty('split'));  // false
    console.log(String.prototype.hasOwnProperty('split'));  // true
  • 相关阅读:
    vue笔记四
    vue笔记三(组件)
    vue笔记二
    Linux04——手动修改IP和关闭防火墙服务
    Linux03——磁盘分区和挂载
    Linux02——解压和压缩
    Linux01——常用命令
    Springmvc-crud-07(springmvc标签错误)
    Springmvc-crud-06(路径忘记加上“/”错误)
    Springmvc-crud-05(路径错误)
  • 原文地址:https://www.cnblogs.com/NanKe-Studying/p/13852348.html
Copyright © 2011-2022 走看看