[JS] Five minutes to understand the object constructor wrapper class

[what is an object]

The explanation given by W3school is

Everything in JavaScript is an object: strings, numbers, arrays, dates, and so on.

In JavaScript, objects are data that have properties and methods.

var obj={
   a:123,
   b:234,
   say:function(){
         console.log('hello')
   }
}

This is a typical js object, it has the properties and methods of the object, and the properties and methods of the object are both readable and writable.

[Object creation method]

1. Object literals

That is the example I gave before var obj={}

2. Constructor

 1) The built-in constructor Object() of the system

var obj =new Object();
obj.name='haha';
obj.sex='male'

2) Custom constructor

a. There is no difference in structure between constructors and functions, but it is stipulated that constructors must follow the naming rules of big camel case.

function Person(){
   this.name='haha',
   this.sex='male'
}

b. The constructor needs to create an object and must use the new operator

var person1 = new Person ();
var person2 = new Person ();

person1 and person2 are exactly the same length, but are two completely separate objects with different addresses.

c. Constructors can pass parameters like ordinary functions 

 
 
function Person(name,sex){
   this.name=name,
   this.sex=sex,
}

var person =new Person('hehe','female')

We found that there is no difference between constructors and ordinary functions, so why it can create objects, the key lies in the new operator, after new, the js engine will treat it as a constructor, and perform the following implicit operations ;

1. Implicitly add a this object var this={} at the front of the function body;

2. Execute this.xxx=xxx;

3. Implicitly returns this

function Person(name,sex){
   //var this={}
   this.name=name,
   this.sex=sex,
   //return this;
}

It is worth mentioning that when you new, it is not an ordinary function. You will find that when you customize a return in the function body, the js engine will choose to ignore it and continue to execute return this

[packaging class]

In the process of learning js, we must have heard a saying that everything is an object, including the W3school mentioned above, which has also made me full of doubts. When we learn js types, we know that js data is divided into For primitive values ​​and reference values, primitive values ​​are immutable, and it cannot have properties and methods, so why do we call it an object too?

This requires the introduction of the concept of a wrapper class, first look at a common example below

var str = 'abcd' ;
console.log(str.length);

We all know that this doesn't give an error, and it will output 4, why is that?

We know that the js engine is very benevolent, it can not give you an error unless you report an error, when you call str.length, it will help you implicitly convert str before, var str =new String('abcd' ), convert it to a string of object type. This object is a temporary object, which will be destroyed when it ends, and the properties on this temporary object will also disappear.

var str = 'abcd';
str.length=2;
console.log(str);

//abcd

These implicitly called procedures are wrapper classes.

Insert one more sentence, we will find that null and undefined are members of the original value. Are they objects? They do not have corresponding constructors to convert them. We typeof(null) will find that it is an object, but neither of them can have properties, so I don't think both are members of the object.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325726345&siteId=291194637