on JavaScript constructors

Source: Internet
Author: User
Tags access properties constructor expression return

Constructors
Constructors in JavaScript are different from constructors in other languages.
Functions that are called through the New keyword are considered constructors.
Inside the constructor-that is, within the called function-this points to the newly created object.
The prototype of this newly created object is directed to the prototype of the constructor.
If the called function does not have an explicit return expression, it implicitly returns the This object-that is, the newly created object.
function Foo () {
This.bla = 1;
}
Foo.prototype.test = function () {
Console.log (THIS.BLA);
};
var test = new Foo ();
The code above calls Foo as a constructor and sets the prototype of the newly created object to Foo.prototype.
An explicit return expression affects the result returned, but is limited to returning an object.
function Bar () {
return 2;
}
New Bar (); Returns the newly created object

function Test () {
This.value = 2;

return {
Foo:1
};
}
New Test (); The returned object
New Bar () returns the newly created object, not the literal value of 2.
So the new bar (). constructor = = Bar, but if you return a numeric object, the result is different, as shown here
function Bar () {
return new number (2);
}
New Bar (). constructor = = Number
The new Test () you get here is the object that the function returns, not the newly created object, as a result of this keyword:
(New Test ()). Value = = undefined
(New Test ()). Foo = = 1
If new is omitted, the function does not return the newly created object.
function Foo () {
This.bla = 1; Get Set global parameters
}
Foo (); Undefined
Although the above example works in some cases, because of how this is working in JavaScript,
This here points to the global object.
Factory mode
In order to not use the new keyword, the constructor must explicitly return a value.

function Bar () {
var value = 1;
return {
Method:function () {
return value;
}
}
}
Bar.prototype = {
Foo:function () {}
};
New Bar ();
Bar ();
The two previous calls to the Bar function are exactly the same, and a newly created object with the method property is returned,
In fact, a closure is created here.
Also note that the new Bar () does not change the prototype of the returned object: that is, the returned object's prototype does not point to Bar.prototype.
Because the constructor's prototype is directed to the new object that was just created, the Bar here does not return the new object (the translator note: Instead, it returns a custom object that contains the method attribute).
In the example above, there is no functional difference between using or not using the New keyword.
Objects created in both of these ways cannot access properties on the Bar prototype chain, as follows:
var bar1 = new Bar ();
typeof (Bar1.method); "Function"
typeof (Bar1.foo); "Undefined"
var bar2 = Bar ();
typeof (Bar2.method); "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.