JS function declaration

Source: Internet
Author: User
Tags js function declaration

<! DOCTYPE html>
<meta charset= "UTF-8" >
<title></title>
<body>
<script type= "Text/javascript" >
function box (num1,num2) {
return num1+num2; General function Declaration method
}
alert (box);

var box=function (num1,num2) {
return num1+num2; Using variable initialization functions
}
alert (box);

var box=new Function (' num1 ', ' num2 ', ' return num1+num2 ');
alert (box); Use the constructor of new to declare


Functions can pass functions


The following example is common, not passed as a function, but passed as the return value of the function.
function box (sum,num) {
return sum+num;
}
function sum (num) {
return num+10;
}

var result=box (sum (10), 10); SUM (10) The return value of the function is passed here, as is the normal variable, no difference
alert (result);//30


To pass the function itself as a parameter, not the result of the function
function box (sum,num) {
return sum (num);
}
function sum (num) {
return num+10;
}
var result=box (sum,10);//This sum is a function that is passed as a parameter to another function instead of the return value of the function.
alert (return);//20

function box (num) {
if (num<=1) {
return 1;
}else{
Return Num*arguments.callee (num-1);//4*3*2*1=24 factorial, recursive
}//Use callee to invoke itself to implement recursion
}
Alert (Box (4));

var color= "Red"; Here, color is the global variable, and this variable is the property of the window.

var box={
Color: "Blue",
Saycolor:function () {
alert (This.color); The color here is the Box property, which is the local variable
Here this, we are determined to represent the box object
}
}
alert (This.color); Red.
Box.saycolor (); Blue.


function box (name,age) {
return name+age;
}
alert (box.length);


function box (num1,num2) {
return num1+num2;
}
Method one: function sum (num1,num2) {//apply and call can impersonate another function
Return box.apply (this,[num1,num2]);//this represents the window scope, "" means the parameter passed
}

Method Two: function sum (num1,num2) {
Return box.apply (this,arguments);//This can be passed as an array
}
Alert (Box (10,10)); 20
Alert (sum (10,10)); 20

function box (num1,num2) {
return num1+num2;
}

function sum (num1,num2) {
Return Box.call (this,num1,num2);//call only pass parameters, others are the same as apply
}
Alert (sum (10,10));


var color= "Red";
var box={
Color: ' Blue '
}
function Saycolor () {
alert (This.color);
}
Saycolor (); Global
Use call is to implement the object impersonating, impersonating box, impersonating window under
Saycolor.call (window); Posing as window red
Saycolor.call (box); Impersonating box, the scope is inside the box object, so color is blue


</script>
</body>

JS function declaration

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.