No more ES6 out.--object several new methods in the __javascript

Source: Internet
Author: User
Tags shallow copy
Object.is (A, b)
  Before talking about this method, first of all, I want to talk about the difference between = = = = = = = = = = = = = = = = = = =
  -_-!
  = =: When the comparison is automatically converted to the type, to compare.
  = =: The only drawback is that Nan is not equal to itself, +0 and 0 equals.

  the ES6 adds object.is (A, B) to this method:

  console.log (+0 = = -0);//true
  console.log (nan = = nan);//false

  Console.log (Object.is (+0,-0)); False
  Console.log (Object.is (Nan, Nan));//true
object.assign (target, source ...)
  You must ask what this function is for.
  Copies the value of all enumerable properties of one or more source objects to the target object. 
  Maybe it's a little bit of a new monk here, listen to me slowly: you declare variable when many of it is like this: (More into the ES6, I use let out) let S1 = {name: ' Dai '} In fact, there are a lot of objects literally hidden, we can make it show the original: Object.getownpropertydescriptor (S1, ' name ')//object {value: "Dai", Writable:true, Enumerable:true, configurable:t
  Rue}//1, Value: Needless to say, is the attribute value. 2, Writable: Can modify//3, enumerable: Whether can be enumerated//4, Configurable: can be configured.
  (for example, delete) In fact, we can use the Object.defineproperty (obj, name, config) let obj = {} in ES5 when declaring variables; Object.defineproperty (obj, ' name ', {value: ' Xiaoming ', Configurable:false, Writable:false, Enumerable:
  False}); Console.log (obj); Object {name: ' xiaoming '} delete obj.name; Error obj.name = "new"; Error for (Let I in obj) {if (Obj.hasownproperty (i)) {console.log (i);//No output, only one property you set the non-enumeration}} large
  Home to ask here and object.assign () what is the relationship, eldest brother you are not digress. The enumerable here is very important, before ES6, ignoring properties that are enumerable false: (1) for in (2) Object.keys () (only their own properties are required, which can be substituted for Forin) (3) JSONA new one is added to Stringify () ES6: Object.assign ();

  (The prototype method in class is also not enumerable) Object.assign () is primarily used to copy objects.
  For example: let S1 = {}; Object.defineproperty (S1, ' name ', {value: ' Xiaoming ', Configurable:true, Enumerable:true, Writable:tru
  e});  Object.defineproperty (S1, ' age ', {value:21, configurable:true, Enumerable:false,//This property is not enumerable writable:
  true});
  Let S2 = object.assign ({},s1); Console.log (S2); 
    The nature of the object.assign () copy of the Object {name: "Xiaoming"} is still a shallow copy, if you need a deep copy: let S1 = {name: ' Xiaoming ', age:21,
  Info: {Childhood: {name: ' Xiao '}, Youth: {name: ' Xiaoming '}}}
  Let S2 = Json.parse (json.stringify (S1)); Console.log (S1 = = = s2); False Console.log (S1.info = = S2.info); False
object.setprototypeof ()
  First of all, __proto__ is an internal property that is used to manipulate prototype.
  Direct use of prototype is not particularly good, so there are several alternative methods:
  1, object.setprototypeof () write operation (ES6)
  2, object.getprototypeof () read operation ( ES5)
  3, object.create () build operation (ES5)

  ---------------------------
    object.getprototypeof
  ------------ ---------------
  Function people () {};
  Let P = new people ();
  Console.log (object.getprototypeof (p) = = People.prototype); True

  --------------------
    object.create ()
  --------------------let
  o = {};
  The above operation is equivalent to let o = Object.create (object.prototype);  

  -----------------------------
    object.setprototypeof ()
  -----------------------------let
  fa = {};
  Let ch = {
    x:10
  }
  object.setprototypeof (ch, FA);
  FA.Y =;
  Console.log (CH.Y); 20

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.