1. About this object
//Add function object method method1
Function.prototype.method1=function(){
alert("function1");
}
function func1(a,b,c){
return a b c;
}
func1.method1(); //Hint: function1
func1.method1.method1(); //Tip: function1
//Add object method getType, including both ordinary objects and function objects
Object.prototype.getType=function(){
return typeof (this);
}
var array1=new Array();
function func1(a,b){
return a b;
}
alert(array1.getType() ); //Prompt: object
alert(func1.getType()); //Prompt: function
//func2 is passed as an object to the formal parameter theFunc of func1 , and then theFunc is called from within func1
function func1(theFunc){
theFunc();
}
function func2(){
alert("ok");
}
func1(func2); // Tip: ok
//When making a function call, in addition to the specified parameters, an implicit object arguments is also created
function func(a,b){
alert(a);
alert(b);
for(var i=0;i
alert(arguments [i]);
}
}
func(1,2,3); //Hint: 1,2,3
/*
Another attribute of the arguments object is callee,
which represents a reference to the function object itself.
This is helpful for implementing recursion of unnamed functions or ensuring the encapsulation of functions.
*/
var sum =function(n){
if(1==n)
return 1;
else
return n arguments.callee(n-1);
}
alert(sum (100)); //Prompt: 5050
/*
JavaScript defines two methods for function objects: apply and call.
Their functions are to The function is bound to another object to run. The only difference between the two is the way to define parameters:
The following is a reference fragment:
Function.prototype.apply(thisArg,argArray);
Function.prototype .call(thisArg[,arg1[,arg2…]]);
As you can see from the function prototype, the first parameter is named thisArg,
that is, the this pointer inside all functions will be Assigned to thisArg,
This achieves the purpose of running the function as a method of another object.
Except for the thisArg parameter, both methods are parameters passed for the Function object.
*/
//Define a function func1 with attribute p and method A
function func1(){
this.p="func1-";
this.A =function(arg){
alert(this.p arg);
}
}
//Define a function func2 with attribute p and method B
function func2(){
this.p="func2-";
this.B=function(arg){
alert(this.p arg);
}
}
var obj1=new func1 ();
var obj2=new func2();
obj1.A("byA"); //Display func1-byA
obj2.B("byB"); //Display func2-byB
obj1.A.apply(obj2,["byA"]); //Display func2-byA, where ["byA"] is an array with only one element, the same below as
obj2.B.apply( obj1,["byB"]); //Display func1-byB
obj1.A.call(obj2,"byA"); //Display func2-byA
obj2.B.call(obj1,"byB "); //Display func1-byB
/*
It can be seen that after method A of obj1 is bound to obj2 to run,
the entire running environment of function A is transferred to obj2, which is the this pointer Points to obj2.
Similarly, function B of obj2 can also be bound to the obj1 object to run.
The last 4 lines of the code show the difference between the parameter forms of the apply and call functions.
*/
/*
is different from the length attribute of arguments.
The function object also has an attribute length.
It represents the length specified when the function is defined. The number of parameters,
instead of the actual number of parameters passed when calling
*/
function sum(a,b){
return a b;
}
alert(sum .length);