Using the constructor in JavaScript to create objects does not require new descriptions _ javascript skills

Source: Internet
Author: User
You can directly create objects in JS using the direct quantity method. The following describes how to define a constructor (function ):

The Code is as follows:

Function Person (name, age ){
This. name = name;
This. age = age;
}
Var p = new Person ('lily', 20 );



It is strange to find that the method of creating a regular object in some library code does not require new. As follows:

The Code is as follows:

Var reg = RegExp ('^ he $ ');



The test shows that if you use or do not use new, the regular objects are returned at the end, and the typeof objects are all "objects ".

The Code is as follows:

Var reg1 = new RegExp ('^ he $ ');
Var reg2 = RegExp ('^ he $ ');
Reg1.test ('hes'); // true
Reg2.test ('hes'); // true
Console. log (typeof reg1); // object
Console. log (typeof reg2); // object



Well, the code runs normally.
If this is the case, no new is written, which saves the amount of code. Is the same for other types? Try String/Number/Boolean.

The Code is as follows:

Var str1 = new String (1 );
Var str2 = String (1 );
Var num1 = new Number ('1 ');
Var num2 = Number ('1 ');
Var boo1 = new Boolean (1 );
Var boo2 = Boolean (1 );
Console. log (typeof str1); // object
Console. log (typeof str2); // string
Console. log (typeof num1); // object
Console. log (typeof num2); // number
Console. log (typeof boo1); // object
Console. log (typeof boo2); // boolean



As you can see, it is different from regular expressions. Regular Expressions are all objects regardless of whether they are new or typeof.
But for the String/Number/Boolean type, the new object typeof returns "object", and the non-new typeof returns "string ".
That is, if new is not applicable, other types can be converted into strings, numbers, and Boolean types.

Now, return to the Person class in the beginning. That is, can we use the new operator to generate objects for classes we write ourselves?

The Code is as follows:

Function Person (name, age ){
This. name = name;
This. age = age;
}
Var p = Person ('lily', 20 );
Console. log (p); // undefined



Undefined is returned, obviously not. Therefore, it is whimsical to create a Person instance without the need of a new one.
What if I have to implement it? In fact, this is also true, as shown below:

The Code is as follows:

Function Person (name, age ){
This. name = name;
This. age = age;
If (this = window ){
Return new Person (name, age );
}
}
Var p = Person ('lily', 20); // object



Slightly changed the Person class. In fact, it distinguishes whether the Person is executed as the constructor or function.
Related Article

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.