JavaScript Learning Notes (vii) JS function Introduction _ Basic knowledge

Source: Internet
Author: User
1. Function Internal Property arguments
Arguments is used to hold the parameters of the function, Arguments.callee point to the function that owns the arguments object
Copy Code code as follows:

Factorial
function factorial (num) {
if (num <= 1) {
return 1;
} else {
Return Num*arguments.callee (num-1); Replace with Agreements.callee
}
}

var truefactorial = factorial;
factorial = function {
return 0;
}
Alert (Truefactorial (5)); 20
Alert (factorial (5)); 0

2. Properties and methods of functions
Length property, representing the number of function arguments

3. Apply () and call () method
The Apply () and call () methods are used to pass the scope of an argument or extension function
Copy Code code as follows:

Passing parameters
function sum (num1,num2) {
return num1+num2;
}
function Callsum (num1,num2) {
Return Sum.call (THIS,NUM1,NUM2); The first argument, this, lists all the parameters
}
Alert (Callsum (10,10)); 20

function CalSum1 (num1,num2) {
Return sum.apply (this,arguments); First argument this, second argument arguments
}
function calSum2 (num1,num2) {
Return sum.apply (this,[num1,num2]); The first argument is this, and the second argument is an array of arguments
}
Alert (CALLSUM1 (10,10)); 20
Alert (callSum2 (10,10)); 20

Copy Code code as follows:

Changing the scope of a function
Window.color = "Red";
var o = {color: "Blue"};
function Saycolor () {
alert (This.color);
}
Saycolor (); Red
Saycolor.call (this); Red
Saycolor.call (window);//red
Saycolor.call (o); Blue
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.