The most characteristic and confusing function in JavaScript is the one that counts.
Let's take a look at common operations
Copy Code code as follows:
function doit () {
.....
}
Doit ();
Functions in JavaScript we can use it as a method
Copy Code code as follows:
var obj=new Object ();
Obj.say=function () {
.....
}
Obj.say ();
And a function is actually an object (that is, an instance of a function type)
Copy Code code as follows:
function result (NUM1, num2) {
return NUM1 + num2;
}
var result = new Function ("Num1", "num2", "return num1+num2");
The above execution effect is the same, and function result can also be written (i.e. function expression)
Copy Code code as follows:
var result=function (num1,num2) {
return num1+num2;
}
The only difference between the two is that a function is a priority, and a functional expression is executed by the code, and there is an array-like arguments object inside each function.
function to execute dynamic parameters, i.e.
Copy Code code as follows:
function result () {
return arguments[0]+arguments[1];
}
Result (1,2);
Arguments frequently used in dynamic transfer parameters
Since the function is an object, it should also be a specific attribute.
Copy Code code as follows:
function person () {
....
}
Person.name= "XXXX";
Person.say=function () {
alert (this.name);
}
Person.say (); Alert ("XXXX")
We can also think of it as a class, and the function body is equivalent to a constructor
Copy Code code 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
Notice to use this.name because this represents the current object, and if direct alert (name) asks for the properties of the Window object, the parameter nm that is passed in can be used directly in the method say, which actually involves the scope chain, Each function body is a scope, the subdomain can access the properties of the parent domain, and the reverse is not (in fact, can be taken, design to the closure of some knowledge, here do not do detailed ...)
Each class can have static properties or methods, compared to some other OO languages, and JavaScript is modeled to achieve that each object shares its properties
Copy Code code as follows:
function person (num) {
.....
}
Person.prototype.name = "YGM";
Alert (new Person (). name);
But the static method of Oo language is called by the class, cannot instantiate itself, in JavaScript because its particularity is opposite
Note that the Name property of the Alertperson here, if the function body does not find the name will be found in the prototype, if found will mask the prototype name directly return its value
In fact, each creation of a function also created a prototype object, and the prototype object is referenced from object, so object is the base class for all objects
We can rewrite the prototype object
Person.prototype=new Parentperson ();
The person's prototype object points to the Parentperson object, and the Parentperson object points to its own prototype object ... and it forms a prototype chain ...
Well, it's written here today ...