JavaScript Learning Notes Function chapter (V): constructor _ Basics

Source: Internet
Author: User

The constructors in Javascript are different than other languages. Any function called by the keyword new can be used as a constructor.
In the constructor body, this points to the newly created object. If a return expression is not displayed in the constructor body, then this is returned by default, which is the newly created object.

Copy Code code as follows:

function Foo () {
This.bla = 1;
}
Foo.prototype.test = function () {
Console.log (THIS.BLA);
};
var test = new Foo ();

The code above invokes Foo as a constructor and points the new object's prototype (__proto__) to Foo.prototype.
If we define the return expression returned in the constructor, the constructor returns the entire expression, but the return expression must be an object.

Copy Code code as follows:

function Bar () {
return 2;
}
New Bar (); A new object
function Test () {
This.value = 2;
return {
Foo:1
};
}
New Test (); The returned object

If new is omitted, then the function will not be able to return a new object.

Copy Code code as follows:

function Foo () {
This.bla = 1; Gets set on the global object
}
Foo (); Undefined

The above example may also be run under some scenarios, but because of the working mechanism of this in Javascript, this will point to the global object.

Factory mode

In order to be able to not use the keyword new, the constructor will have to display a return value.

Copy Code code as follows:

function Bar () {
var value = 1;
return {
Method:function () {
return value;
}
}
}
Bar.prototype = {
Foo:function () {}
};
New Bar ();
Bar ();

In the example above, the effect of calling function Bar without new is the same, and will return a new object that contains method methods, which is actually a closure.
It is important to note here that the new Bar () will not return Bar.prototype, but rather the prototype object of the function method within the returns expression.
In the example above, the use of new or not is functionally different.

Create a new object from Factory mode

We are often reminded not to use new because forgetting its use will result in an error.
In order to create an object, we prefer to use the factory pattern and construct a new object in the Factory mode.

Copy Code code as follows:

function Foo () {
var obj = {};
Obj.value = ' blub ';

var private = 2;
Obj.somemethod = function (value) {
This.value = value;
}

Obj.getprivate = function () {
return private;
}
return obj;
}

Although the previous code is less prone to error than using new, and is more convenient when using private variables, there are also some bad places:

Because the prototype object cannot be shared, more memory is required.
To implement inheritance, the factory pattern needs to copy all the methods of another object or as a prototype of the new object.
Abandoning the prototype chain is just to avoid using new, which seems to contradict the spirit of the Javascript language.

Summarize

Although it may be easier to create errors using new, this is not the reason to discard the use of the prototype chain. As for the final approach, this needs to be based on the needs of the application. The best way is to choose a style and stick to it.

Simply put, the constructor initializes an instance object, and the object's prototype property inherits an instance object.

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.