Object creation method

Object literals

  • Is the simplest and most common way to create an object
var obj = {};
Copy the code

The constructor

  • A constructor is a function that provides a template for generating an object and describes the basic structure of the object. A constructor can generate multiple objects, all of which have the same structure
  • A constructor is a normal function. To distinguish it from other normal functions, capitalize the constructor’s function name
  • Used in the body of a functionthisThe keyword represents the object instance to be generated
  • Must be used when generating objectsnewCommand to call the constructor, so the constructor is more reasonable to think of asA constructor call to a function
  • newThe command executes a constructor and returns an object instance. usenewCommand, the function call that follows it is not a normal call, but executes the following steps in sequence

    Create an empty object as an instance of the object to be returned. 2. Assign the empty object to the constructor’s prototype property. 3. All operations on this will take place on the empty object

  • There are two types of constructors:System built-in constructorsandCustom constructor
  • Built-in system constructors
    • The constructor for creating the object isObject() -> var obj = new Object(), its function andvar obj = {}The function of theta is the same
    • The system comes with a lot more:Number(),String(),Array(),Boolean(),Date()
  • Custom constructors: The most common type of constructor:function Person() {}
    function Person(name) {
       // var this = {}
       this.name = name;
       // return this
    }
    var person = new Person('Tina')
    Copy the code
    • If you manually create an object at the first line of the constructor, for examplethatObject and finally returnedthatWhere there isthisIt doesn’t work, it’s used to assign a value to a propertythat
    function Person(name) {
       var that = {name: 'aa'}
       that.name = name;
       return that
    }
    var person = new Person('Tina')
    Copy the code
  • Note:If the object is returned, thenthisInvalid, but if the final display returns the original value, thenthisStill valid
    function Person() {
       var that = {}
       that.name = "aa";
       this.name = 'bb';
       return 123;
    }
    var person = new Person()
    person.name // 'bb'
    Copy the code
  • If for ordinary functions (Inside there is nothisKeyword function) to usenewCommand, an empty object is returned
    function Keith() {
      return 'this is a message';
    }
    var boy = new Keith();
    console.log(boy); // Keith {}
    Copy the code

    In the code above, for ordinary functionsKeithusenewThe command creates an empty object. This is becausenewThe command always returns an object, either an instance object or an instance objectreturnStatement specifies an object or array. In this case,returnThe statement returns a string, sonewThe command ignores the statement

Object. The create (prototype)

For creating an Object, it is more recommended to use the literal method, because creating an Object using new Object() requires finding the Object layer by layer through the scope chain, but using the literal method does not have this problem

Add, delete, change and check attributes

  • increase: YesObject name + dot + attribute nameMethod to add new attributes and assign values to objects
  • changeThe operation to modify is the same as the operation to add, just call the same property name and assign a new value
  • check:Object name + dot + attribute name,Object name [attribute name]
  • delete:deleteThe operator is used to delete attributes

Packaging object

There are three types of wrapper objects in JavaScript. Their corresponding constructors are String, Number, and Boolean. These wrapper objects correspond to three primitive types: String, Number, and Boolean. And primitive data types are not attributes and methods, they are pure value, if want to obtain their properties or call a method, it requires packing objects, with the help of primitive types will first through its packaging corresponding constructor into object, and then use this object to call methods or use the property, after the original type is still the original type, The newly created wrapper object is destroyed

Note: undefined and null are unwrapped objects

The original value cannot be changed. Only objects have properties and methods.

var str = 'abcd';
console.log(str.length) / / 4
Copy the code

The original value of the string should not have the length attribute, but you can actually check the length attribute

This involves something called a wrapper class, which automatically wraps STR into a string object with properties and methods before the line is called, and then destroys the string object after the line is executed

var str = 'abcd';
// var str1 = new String('abcd')
str.length // str1.length
/ / destroy str1
Copy the code

This is why str.length = 2; Str. length is still 4. If str.length = 2, create a String object with the value of ‘abcd’. Change the length of the object to ‘abcd’. When str.length = 2, the implicitly created string object is destroyed, so the printed str.length remains 4

var str = 'abcd';
// var str1 = new String('abcd')
str.length = 2 // str1.length = 2
/ / destroy str1
console.log(str.length) // abcd length is still 4
Copy the code

The same is true for other types of data. When attributes are added to raw values, they are implicitly wrapped as objects, and then destroyed after assigning the attribute value