Encapsulated object

  • Prevent objects from extending object.preventextensions (), and the corresponding object.isextensible () method checks whether the incoming Object isExtensible, returning a Boolean value of true and false.

    let obj = { a: 1 } Object.preventExtensions(obj); // return the value of obj itself obj.b = 2; console.log(obj); // -> {a: 1} object.defineProperty (obj, 'c', {// -> Value: 3}) console.log(Object.isextensible (obj)); // -> falseCopy the code
  • The Object Object, Object.seal(), is sealed by calling Object.preventExtensions(), and a loop that sets the traversal property to false. Object.issealed (), check whether the Object isSealed.

    let obj = { a: 1, b: 2 } Object.seal(obj); Obj. C = 3; console.log(obj); // -> {a: 1, b: 2} delete obj.a; console.log(obj); // -> {a: 1, b: 2} obj.a = 3; console.log(obj); // -> {a: 3, b: 2} console.log(Object.isSealed(obj)); // -> trueCopy the code
  • Freeze objects, object.freeze (), which cannot be written, extended, or deleted. Object.isfrozen (), which checks whether the Object isFrozen.

    let obj = { a: 1, b: 2, c: { name: 'foo' } } Object.freeze(obj); Object.isFrozen(obj); // -> true // Shallow copy freeze obj.c.name = 'bar'; console.log(obj); // -> {a: 1, b: 2, c: {name: 'bar'}} for(let key in obj){ if(typeof(obj[key]) === 'object' && obj[key] ! == null){ deepFreeze(obj[key]); }}}Copy the code

is

  • Object.is() and === both call the sameValue algorithm internally, but there are two differences.

    console.log(NaN === NaN); // -> false
    console.log(+0 === -0); // -> ture
    
    console.log(Object.is(NaN, NaN)); // -> true
    console.log(Object.is(+0, -0)); // -> false
    Copy the code

Merge objects

  • Object.assign(tar, … Sources),tar is the target object, the source object can have more than one, the return value is the target object.

    let o = { name: 'foo' } let t = {}; let copy = Object.assign(t, o); console.log(copy === t); Console.log (t); // -> true; // -> {name: "foo"}Copy the code
  • If a property with the same name appears during merge/copy, the latter overwrites the former.

    let tar = {
      a: 1,
      b: 1,
    }
    let s1 = {
      b: 2,
      c: 2
    }
    let s2 = {
      c: 3
    }
    Object.assign(tar, s1, s2);
    console.log(tar); // -> {a: 1, b: 2, c: 3}
    Copy the code
  • An error is reported when the first argument is not an object. In particular, it calls its corresponding wrapper class to be converted to an object. If the second argument is not an object, the first object is returned. If the attributes that can be converted to an object are enumerable, they can be merged; otherwise, no processing is done.

    Object.assign(null, {a: 1}); // -> TypeError
    Object.assign(undefined, {a: 1}); // -> TypeError
    
    Object.assign(1, {a: 1}); // -> Number {1, a: 1}
    Object.assign('1', {a: 1}); // -> String {"1", a: 1}
    Object.assign(true, {a: 1}); // -> Boolean {true, a: 1}
    // --------------------------------------------------
    Object.assign({a: 1}, undefined); // -> {a: 1}
    Object.assign({a: 1}, null); // -> {a: 1}
    Object.assign({a: 1}, 1); // -> {a: 1}
    Object.assign({a: 1}, '33'); // -> {0: "3", 1: "3", a: 1}
    Object.assign({a: 1}, true); // -> {a: 1}
    Copy the code
  • When used for copying, inherited properties and non-enumerable properties cannot be copied and are shallow copies.

    Let obj = object.create ({a: 1}, {b: {value: 2}, c:); // object.create () creates an Object, the first parameter specifies the prototype of the Object, the second parameter is an Object to configure the Object properties. { value: 3, enumerable: true } }) console.log(obj); // -> {c: 3, b: 2} let copy = Object.assign({}, obj); console.log(copy); // -> {c: 3}Copy the code
  • Arrays are also special objects that have a pit when merged.

    let res = Object.assign([1, 2, 3], [4, 5]); Console. log(res); // Console. log(res); // Console. log(res); // -> [4, 5, 3]Copy the code

The property descriptor s

  • Chapter introduces the single descriptor and configuration of a single attribute, defineProperties () and getOwnPropertyDescriptors (), respectively, to configure multiple attributes and obtain all the description.

    let obj = {}; Object.defineProperties(obj, { a: { value: 1, writable: true }, b: { value: 2, writable: false } }) console.log(obj); Object.getOwnPropertyDescriptors(obj); Let source = {get foo () {//... }, set foo (val) { // ... } } let tar = {}; Object.assign(tar, source); Object.defineProperties(tar, Object.getOwnPropertyDescriptors(source));Copy the code