JavaScript Basic Concepts Elementary Tutorial Forum Posted Learning Records _ Basics

Source: Internet
Author: User
Tags function prototype
1. About this object

Copy Code code as follows:

View Plaincopy to Clipboardprint?
var obj1=new Object ();
var obj2=new Object ();

Add attribute p to all two objects, 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 the this pointer is pointing to the obj1
}

Call the Obj1 Getp method, and the results are shown as 1
OBJ1.GETP ();

Getp method to make Obj2 Getp method equal to Obj1
OBJ2.GETP=OBJ1.GETP;

Call the Obj2 Getp method, and the results are shown as 2
OBJ2.GETP ();
var obj1=new Object ();
var obj2=new Object ();

Add attribute p to all two objects, 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 the this pointer is pointing to the obj1
}

Call the Obj1 Getp method, and the results are shown as 1
OBJ1.GETP ();

Getp method to make Obj2 Getp method equal to Obj1
OBJ2.GETP=OBJ1.GETP;

Call the Obj2 Getp method, and the results are shown as 2
OBJ2.GETP ();


2. About function objects


Copy Code code as follows:

Adding a 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 (); Hint: function1



Add Object method gettype, including both normal and function objects
Object.prototype.gettype=function () {
Return typeof (This);
}
var array1=new Array ();
function Func1 (a,b) {
return a+b;
}
Alert (Array1.gettype ()); Tip: Object
Alert (Func1.gettype ()); Hint: function



Func2 is passed as an object to the FUNC1 's formal parameter thefunc, which is then invoked by Thefunc inside the func1.
function Func1 (thefunc) {
Thefunc ();
}
function Func2 () {
Alert ("OK");
}
Func1 (FUNC2); Hint: OK



When a function call is made, an implied object is created in addition to the specified arguments arguments
function func (a,b) {
alert (a);
alert (b);
for (Var i=0;i<arguments.length;i++) {
Alert (Arguments[i]);
}
}
Func (1,2,3); Hint: 1,2,3



/*
Another property of the arguments object is callee,
It represents a reference to the function object itself.
It is advantageous to realize the recursion of nameless function or guarantee the encapsulation of function.
*/
var sum=function (n) {
if (1==n)
return 1;
Else
Return N+arguments.callee (n-1);
}
Alert (sum (100)); Tip: 5050



/*
JavaScript defines two methods for function objects: apply and call.
All they do is bind the function to another object, which differs only in the way that the parameter is defined:
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 implements the purpose of running a function as a method of another object.
Two methods except the Thisarg parameter, are the parameters passed for the function object.
*/

Defines a function func1, with attribute p and method a
function Func1 () {
this.p= "func1-";
This. A=function (ARG) {
alert (THIS.P+ARG);
}
}
Defines 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"); Show Func1-bya
Obj2. B ("Byb"); Show Func2-byb
Obj1. A.apply (obj2,["bya"]); Displays Func2-bya, where ["Bya"] is an array of only one element, the same as
Obj2. B.apply (obj1,["BYB"]); Show Func1-byb
Obj1. A.call (Obj2, "bya"); Show Func2-bya
Obj2. B.call (obj1, "byb"); Show Func1-byb
/*
As can be seen, Obj1 's method A is bound to obj2 after running,
The operating environment of the entire function A is shifted to obj2, that is, the this pointer points to OBJ2.
Similarly, Obj2 's function B can be bound to a Obj1 object to run.
The last 4 lines of code show the difference between the form of apply and call function arguments.
*/



/*
Unlike the length property of the arguments,
The function object also has a property length,
That represents the number of parameters that are specified when the function is defined.
Number of parameters actually passed when not invoked
*/
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.