Home > Web Front-end > JS Tutorial > How to use constructor to create objects in JavaScript without new

How to use constructor to create objects in JavaScript without new

高洛峰
Release: 2016-11-26 09:14:42
Original
1103 people have browsed it

In JS, objects can be created directly using direct variables. What is discussed here is the case of defining a constructor (function). As follows
function Person(name, age) {
this.name = name;
this.age = age;
}
var p = new Person('lily', 20);

Found that some library codes create regular expressions The object method does not require new, which is strange. As follows
var reg = RegExp('^he$');

The test found that whether new is used or not, the final returned objects are regular objects, and typeof them are all "object".
var reg1 = new RegExp('^he$');
var reg2 = RegExp('^he$');
reg1.test('he'); // true
reg2.test('he'); // true
console.log(typeof reg1); // object
console.log(typeof reg2); // object

Well, it’s good, the code runs normally.
If this is the case, simply don’t write new, which will also save the amount of code. Is this true for other types as well? Try String/Number/Boolean.
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

You can see that it is different from the regular situation. Regularly, regardless of whether it is new or not, typeof is followed by object.
But for String/Number/Boolean types, the new object typeof returns "object", and the non-new typeof returns "string".
That is, when new is not applicable, other types can be converted into string, number and Boolean types respectively.

Okay, let’s go back to the Person class at the beginning of the chapter. That is, can the classes we write ourselves generate objects without using the new operator?
function Person(name, age) {
this.name = name;
this.age = age;
}
var p = Person('lily', 20);
console.log(p); // undefined

Returning undefined is obviously not possible. Therefore, it is fanciful to create a Person instance without using new.
What if it must be realized? In fact, it works, 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 modified the Person class. In fact, it is distinguished internally whether Person is executed as a constructor or a function.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template