Analysis of all evil function instances in javascript

Source: Internet
Author: User

The most distinctive and confusing function in javascript is regarded as a function.
Next, let's take a look at common operations.
Copy codeThe Code is as follows:
Function doit (){
.....
}
Doit ();

Functions in javascript can be used as methods.
Copy codeThe Code is as follows:
Var obj = new Object ();
Obj. say = function (){
.....
}
Obj. say ();

Function is actually an object (that is, a Function-type instance)
Copy codeThe Code is as follows:
Function result (num1, num2 ){
Return num1 + num2;
}

Var result = new Function ("num1", "num2", "return num1 + num2 ");
The above execution results are the same, and function results can be written in this way (that is, function expressions)
Copy codeThe Code is as follows:
Var result = function (num1, num2 ){
Return num1 + num2;
}

The only difference between the two statements is that the function is executed first, and the function expression is executed only after the code is executed. In addition, each function has an arguments object similar to an array.
Function execution dynamic parameters, that is
Copy codeThe Code is as follows:
Function result (){
Return arguments [0] + arguments [1];
}
Result (1, 2 );

Arguments is often used in dynamic parameter passing.
Since function is an object, it should also have specific attributes.
Copy codeThe Code is as follows:
Function person (){
....
}
Person. name = "xxxx ";
Person. say = function (){
Alert (this. name );
}
Person. say (); // alert ("xxxx ")

We can also regard it as a class, and the function body is equivalent to the constructor.
Copy codeThe Code is as follows:
Function Person (nm ){
This. name = nm;
This. say = function (){
Alert (nm );
Alert (this. name );
}
}
Var p1 = new Person ("ygm1 ");
P1.say (); // alert ygm1 ygm1
Var p2 = new Person ("ygm2 ");
P2.say (); // alert ygm2 ygm2

Note that this is used here. name because this represents the current object. If alert (name) is used to evaluate the properties of the window object, the passed parameter nm can be directly used in the method say, in fact, this involves the scope chain. Each function body is a scope. The subdomain can access the attributes of the parent domain, but in turn it cannot (in fact, it can also be obtained, and some knowledge about closures is designed, I will not explain it here ..)
Compared with other OO languages, each class can have some static attributes or methods, while javascript simulates them through prototype to achieve that each object shares its attributes.
Copy codeThe Code is as follows:
Function Person (num ){
.....
}
Person. prototype. name = "ygm ";
Alert (new Person (). name );

However, static methods in OO language are called by classes and cannot be instantiated. javascript has the opposite particularity.
Note the alertPerson name attribute here. If the name is not found in the function body, it will be searched in the prototype. If it is found, it will shield the name in the prototype from returning its value directly.
In fact, each time a function is created, a prototype object is also created, and the prototype object references the object. Therefore, the object is the base class of all objects.
We can rewrite the prototype object.
Person. prototype = new ParentPerson ();
The prototype object of Person points to the ParentPerson object, and the ParentPerson object points to its own prototype object..., which forms the prototype chain...
Now, let's write it here today...

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.