The Arguments,callee,caller,call,appy in JavaScript

Source: Internet
Author: User

<script language= "JavaScript" >
/*
* Demonstrate the use of arguments, how to get real parameters and number of shapes
*/
function Argtest (a,b,c,d){
var Numargs = arguments.length; Gets the numeric value of the passed parameter.
var Expargs = argtest.length; Gets the numeric value of the desired parameter.
Alert ("Real parameter:" +numargs)
Alert ("Number of shapes:" +expargs)

Alert (Arguments[0])
Alert (argtest[0])//undefined no such usage
}
Argtest (ON)
Argtest (1,2,3,4,5)

/*
* Arguments not arrays (array Class)
*/

Array.prototype.selfvalue = 1;
function testaguments (){
Alert ("arguments.selfvalue=" +arguments.selfvalue);
}
Alert ("Array.sefvalue=" +new Array (). Selfvalue);
Testaguments ();





/*
* Demonstrates the caller property of a function.
* Description: (current function). Caller: Returns a reference to the function that called the current function
*/

function Callerdemo (){
if (Callerdemo.caller){
var a= callerdemo.caller.arguments[0];
alert (a);
} else{
Alert ("This is a top function");
}
}
function Handlecaller (){
Callerdemo ();
}

Callerdemo ();
Handlecaller ("Parameter 1", "Parameter 2");


/*
* Demonstrates the callee property of a function.
* Description: Arguments.callee: The initial value is the function object being executed for the anonymous function
*/
function Calleedemo (){
alert (Arguments.callee);
}
Calleedemo ();
(function (ARG0,ARG1) {alert ("Number of shapes:" +arguments.callee.length)}) ();


/*
* Demonstrates the use of the Apply,call function
* Description: The role is to bind the function to another object to run, the two are only defined in the way the parameters differ:
* Apply (Thisarg,argarray);
* Call (Thisarg[,arg1,arg2 ...]);
* That is, the this pointer inside all functions will be assigned a value of Thisarg
*/

function Objecta (){
Alert ("Execute Objecta ()");
Alert (arguments[0]);
This.hit=function (msg){alert (msg)}
    this.info= "I'm from Objecta"
 }
 
 function objectb () {
     alert ("Executive OBJECTB ()");
    //calls the Objecta () method, and all this in the Objecta constructor is replaced by this in OBJECTB
     objecta.apply (this,arguments);//objecta.call (this);
    alert (This.info);
 }
&NBSP;//OBJECTB (' parameter 0 ');


 var value= "global  variable";
 function obj () {
     This.value= "Object! ";
 }
 function fun1 () {
     alert (this.value);
 }
 //fun1 ();
 //fun1.apply (window);  
 //fun1.apply (New obj ());  
</script>

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.