JavaScript prototype properties in-depth introduction to _javascript tips

Source: Internet
Author: User
Each function is created by default with a prototype property, which contains a constructor property, and a hidden property __proto__ that points to object objects. The value of the constructor property is the object of the function. Called with new in front of a function, a new object (linked by the __proto__ property) that hides the prototype member of the function is created, and the This of the function is bound to the new object.
The function always returns a value, returns undefined if no return value is specified, and returns this if it is called as a constructor and the return value is not an object; If the return value is an object, it is meaningless as a constructor!
[JavaScript]
Copy Code code as follows:

function A () {
THIS.P = ' haha ';
return {p: ' Heihei '};
}
var a = new A ();
function A () {
THIS.P = ' haha ';
return {p: ' Heihei '};
}
var a = new A ();
alert (A.P);//Show ' Heihei ' as the effect of var a = a ();

Function A directly call a function b,b this binding to the global object instead of its external function A, this is a JS design error. We have to solve this problem in other ways, such as using a variable (usually that) in a to hold a reference to this scope of a.
The JS function has a length property that represents the number of formal parameters specified when the function is defined.
The Arguments property of a function contains all the arguments passed in when the function is invoked, regardless of whether the formal parameters are defined in the declaration of the function, arguments is not an array, but an "array-like" object (running the arguments instanceof array in a function); returns false). It can be converted to a JS array by Array.prototype.slice.apply (arguments).
To add a method to a JavaScript function's prototype, all of the (constructed) functions are available! For example, you can add a method to the stereotype of the constructor function of the JS functions, and all functions, including object, number, and so on, inherit the method, which is powerful:
[JavaScript]
Copy Code code as follows:

Function.prototype.method = function (name, func) {
This.prototype[name] = func;
return this;
};
Function.prototype.method = function (name, func) {
This.prototype[name] = func;
return this;
};

In this way, by invoking the Object.Method method, you can add new methods to all JS objects (including function objects), call the Number.method method, and add new methods to all numeric types, and here's one example. Note Objects of type object, number, and so on do not inherit method methods at this time. If you want to do this, you can run a statement similar to the following:
[JavaScript]
Copy Code code as follows:

Object.Method (' method ', Object.Method);
Object.Method (' method ', Object.Method);

We can add a new method to the numeric type by modifying the prototype of the numeric type, where we use the method described in the previous article to add a negative method to the prototype of number:
[JavaScript]
Copy Code code as follows:

Number.method (Negative,function () {
return 0–this;
})
Number.method (Negative,function () {
return 0–this;
})

The method is called a little bit around. In JavaScript syntax, a number is followed directly by the DOT, followed by the syntax of the method invocation, which means that 3.negative () is wrong. To invoke a method of a numeric type, you need to add n spaces (n>=1) after the number, or enclose the number in parentheses, force it into an expression, call the method, or simply define a numeric variable, or call the method directly. That is to say, the following is the correct wording:
[JavaScript]
(3). negative ();
3. negative ();
var n = 3; N.negative ();
3[' negative '] ();
(3). negative ();
3. negative ();
var n = 3; N.negative ();
3[' negative '] ();
When you use a function expression method to define a function, the function name that follows it can be used to recursively invoke itself, and the name will not be overwritten! Let's look at the following example,
[JavaScript]
Copy Code code as follows:

function A (n) {
if (n>1)
Return a (n-1) +1;
Else
return 1;
};
function A (n) {
if (n>1)
Return a (n-1) +1;
Else
return 1;
};

The above code defines a function a, and its internal recursion calls itself; now we use a new reference AA to point to function A, and then change the original A, such as to an integer 1, and then call the function AA, as shown in the following code:
[JavaScript]
Copy Code code as follows:

var aa = A;
A = 1;
AA (3);
var aa = A;
A = 1;
AA (3);

The console complains: Typeerror:property ' A ' of Object [object Window] is not a function; Obviously, the original recursive function has been corrupted. On this issue, we can use Arguments.callee.caller to replace a in the interior of function A, or to define a function with a function expression:
[JavaScript]
Copy Code code as follows:

var B = function A (n) {
if (n>1)
Return a (n-1) +1;
Else
return 1;
};
var bb = b;
A = 3;
BB (3);
var B = function A (n) {
if (n>1)
Return a (n-1) +1;
Else
return 1;
};
var bb = b;
A = 3;
BB (3);

At this point, the BB function can correctly return the results we want.
To improve the encapsulation of JavaScript functions, we can define a functional constructor, and here is an example:
Copy Code code as follows:

[JavaScript]
var funccons = function (spec) {
var that = {};
That.getname = function () {
return spec.name;
};
That.says = function () {
return Spec.saying | | '';
};
return to that;
};
var MyFunc = funccons ({name: ' Neareast '});
var funccons = function (spec) {
var that = {};
That.getname = function () {
return spec.name;
};
That.says = function () {
return Spec.saying | | '';
};
return to that;
};
var MyFunc = funccons ({name: ' Neareast '});

In this way, we can define some private variables (such as dictionary tables) and functions in the constructor without exposing them all to the outside.
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.