JavaScript face questions summary series (b)

2. JavaScript objects

2.1 built-in objects

  • JavaScript Object is the parent of all objects;
  • Package data class object: Object, Array, Boolean, Number, String;
  • Other objects: Function, Arguments, Math, Date, RegExp, Error, JSON, the global object;

2.2 define the way objects

  • Object literal: var obj = {};
  • Constructor: var obj = new Object ();
  • Object.create(); var obj = Object.create(Object.prototype);

2.3 and create objects by the difference between literal way through the new way

  • Creating objects by literal way, does not call the Object () constructor, Introduction and better performance;
  • When you create an object using the new operator, call Object () constructor method call essentially involves traversing the method __proto__ chain when found a method, the method will produce the necessary call stack information, the method ends , you need to release the stack, it is better to create objects using literal way in performance.

2.4 How to determine the two objects are equal

Can be converted to a string is determined. For example:

obj = {
    a: 1,
    b: 2
}
obj2 = {
    a: 1,
    b: 2
}
obj3 = {
    a: 1,
    b: '2'
}

JSON.stringify(obj) == JSON.stringify(obj2);//true
JSON.stringify(obj) == JSON.stringify(obj3);//false

2.5 new operator

During a call to the new operator, the execution of the following four steps:

  1. A new object is generated;
  2. Link to prototype:
let obj = new Object();
let Con = [].shift.call(arguments);
obj.__proto__ = Con.prototype
  1. Binding this: apply;
  2. Returns a new object;

One sentence is:

Create a new operator empty object that points to the prototype prototype constructor returns the object constructor is executed.

Shallow vs. deep copy 2.6

  • What is shallow copy, what is a deep copy?
    Shallow vs. deep copy, herein generally refers to assign one object to another variable A B. When A modified, if B also followed changes, this is a shallow copy, if B does not change, this is called a deep copy.
  • How shallow copy can be achieved
    can be achieved through direct shallow copy of the object is assigned to a variable. For example:
let a = [1, 2, 3];
let b = a;
a[0] = 4;
b[0] // 4
  • How deep copy may
    1.Object.assign (); see example:
let a = {
    age: 1
}
let b = Object.assign({}, a)
a.age = 2
console.log(b.age) // 1

2. Expand the operator. For example:

let a = {
    age: 1
}
let b = {...a}
a.age = 2
console.log(b.age) // 1
  1. JSON methods:

    Mainly achieved through deep copy JSON.parse () and JSON.stringify () match. For example:

let a = {
  age: 1,
  jobs: {
    first: 'FE'
  }
}
let b = JSON.parse(JSON.stringify(a))
a.jobs.first = 'native'
console.log(b.jobs.first) // FE
  1. jQuery the extend () methods
    due to the relatively small item with jQuery Now, when used alone may check to the API.
  2. Recursion
    This is mainly for elements of the object is not a one-dimensional element, the element itself may be an object or array, or even sub-element sub-elements are objects or arrays of time, fundamentally up to achieve a deep copy.

Guess you like

Origin www.cnblogs.com/zxxsteven/p/11719906.html