Basic concepts of JavaScript basic introduction to Forum posts-Basic Knowledge

Source: Internet
Author: User
The basic concepts of JavaScript forum post is a summary of some advanced js skills. 1. About this object

The Code is as follows:


View plaincopy to clipboardprint?
Var obj1 = new Object ();
Var obj2 = new Object ();

// Add the property p to both objects, which is 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
}

// Call the getP method of obj1. The result is 1.
Obj1.getP ();

// Make the getP method of obj2 equal to the getP method of obj1
Obj2.getP = obj1.getP;

// Call the getP method of obj2. The result is 2.
Obj2.getP ();
Var obj1 = new Object ();
Var obj2 = new Object ();

// Add the property p to both objects, which is 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
}

// Call the getP method of obj1. The result is 1.
Obj1.getP ();

// Make the getP method of obj2 equal to the getP method of obj1
Obj2.getP = obj1.getP;

// Call the getP method of obj2. The result is 2.
Obj2.getP ();



2. Function objects


The Code is as follows:


// Add the function object method method1
Function. prototype. method1 = function (){
Alert ("function1 ");
}
Function func1 (a, B, c ){
Return a + B + c;
}
Func1.method1 (); // prompt: function1
Func1.method1. method1 (); // prompt: function1



// Add the object method getType, including both common 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



// As an object, func2 is passed to theFunc, which then calls theFunc internally.
Function func1 (theFunc ){
TheFunc ();
}
Function func2 (){
Alert ("OK ");
}
Func1 (func2); // prompt: OK



// When calling a function, in addition to the specified parameters, an implicit object arguments is also created.
Function func (a, B ){
Alert ();
Alert (B );
For (var I = 0; ialert (arguments [I]);
}
}
Func (, 3); // prompt:, 3



/*
Another attribute of the arguments object is callee,
It indicates a reference to the function object itself.
This facilitates recursion of unknown functions or ensures function encapsulation.
*/
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.
They are used to bind a function to another object for running. The two are different only when defining parameters:
The following is a reference clip:
Function. prototype. apply (thisArg, argArray );
Function. prototype. call (thisArg [, arg1 [, arg2…]);

From the function prototype, we can see that 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 a function as another object.
The two methods except the thisArg parameter are the parameters passed for the Function object.
*/

// Define A function func1 with the property p and method
Function func1 (){
This. p = "func1 -";
This. A = function (arg ){
Alert (this. p + arg );
}
}
// Define a function func2 with the property 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 the func2-byA, where ["byA"] is an array with only one element, the same below
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 for running,
The runtime environment of function A is transferred to obj2, that is, this Pointer Points to obj2.
Similarly, function B of obj2 can be bound to the obj1 object to run.
The last four lines of the Code show the differences between the parameters of the apply and call functions.
*/



/*
Different from the length attribute of arguments,
The function object also has an attribute length,
It indicates the number of parameters specified during function definition,
Number of parameters actually passed when not called
*/
Function sum (a, B ){
Return a + B;
}
Alert (sum. length );

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.