Home > Web Front-end > JS Tutorial > Learning record of the basic explanation forum post of JavaScript basic concepts_Basic knowledge

Learning record of the basic explanation forum post of JavaScript basic concepts_Basic knowledge

WBOY
Release: 2016-05-16 18:55:49
Original
825 people have browsed it

1. About this object

Copy code The code is as follows:

view plaincopy to clipboardprint ?
var obj1=new Object();
var obj2=new Object();

//Add attribute p to both objects, and equal 1 and 2 respectively
obj1 .p=1;
obj2.p=2;

//Add a method to obj1 to display the value of p
obj1.getP=function(){
alert(this .p); //On the surface this pointer points to obj1
}

//Calling the getP method of obj1, the result is displayed as 1
obj1.getP();

//Make obj2’s getP method equal to obj1’s getP method
obj2.getP=obj1.getP;

//Call obj2’s getP method, the result is displayed as 2
obj2.getP() ;
var obj1=new Object();
var obj2=new Object();

//Add attribute p to both objects, and equal to 1 and 2 respectively
obj1 .p=1;
obj2.p=2;

//Add a method to obj1 to display the value of p
obj1.getP=function(){
alert(this .p); //On the surface this pointer points to obj1
}

//Calling the getP method of obj1, the result is displayed as 1
obj1.getP();

//Make obj2’s getP method equal to obj1’s getP method
obj2.getP=obj1.getP;

//Call obj2’s getP method, the result is displayed as 2
obj2.getP() ;


2. About function objects


Copy code The code is as follows:

//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;ialert(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);
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template