Use hidden new to create object _jquery

Source: Internet
Author: User
Most of the time we write classes like this and then use new to create objects.
Copy Code code as follows:

function Person (name,age) {
This.name=name;
This.age=age;
}
person.prototype={
Setname:function (n) {this.name=n;},
Getname:function () {return this.name;}
}
var p = new Person (' Jack ', 25);

Change to this.
Copy Code code as follows:

function Person (name,age) {
The condition is changed to (This==window) or (this==self) or (This.constructor!=object)
if (!this.setname) {
Return to new person (name,age);
}
This.name=name;
This.age=age;
}
person.prototype={
Setname:function (n) {this.name=n;},
Getname:function () {return this.name;}
}
var p = person (' Jack ', 25);

Note that this class has more of the following than the top write class method
Copy Code code as follows:

if (!this.setname) {
Return to new person (name,age);
}

Well, the way you create instances (objects) of a class becomes the following
Copy Code code as follows:

var p = person (' Jack ', 25);

This way of creating (function calls) is less "new_" than the above, new and whitespace, actually new within the class. In this way, you can reduce by 4 byte each time you create an object.
If the inside of the class if the condition is replaced by prototype properties, such as THIS.name. The program prompts for an error: too much recursion
Copy Code code as follows:

function person (name,age) {
if (!this.name) {
return new person (name, Age);
}
This.name=name;
This.age=age;
}
person.prototype={
Setname:function (n) {this.name=n;},
Getname:function () {return this.name;}
}
var p = person (' Jack ', 25);

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.