Home > Web Front-end > JS Tutorial > body text

JavaScript study notes (7) Introduction to js functions_basic knowledge

WBOY
Release: 2016-05-16 17:52:28
Original
880 people have browsed it

1. The internal attribute arguments
arguments of the function is used to save the parameters of the function. arguments.callee points to the function owning the arguments object

Copy code The code is as follows:

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

var trueFactory = factorial;
factorial = function {
return 0;
}
alert(trueFactorial(5)); //20
alert(factorial(5)); //0

2. Function attributes and method
length attribute, indicating the number of function parameters

3. apply() and call() methods
apply() and call() methods are used to pass parameters or expand functions Scope
Copy code The code is as follows:

//Pass parameters
function sum( num1,num2) {
return num1 num2;
}
function callSum(num1,num2) {
return sum.call(this,num1,num2); //The first parameter this, List all parameters later
}
alert(callSum(10,10)); //20

function calSum1(num1,num2) {
return sum.apply(this,arguments) ; //The first parameter this, the second parameter arguments
}
function calSum2(num1,num2) {
return sum.apply(this,[num1,num2]); //First parameter this, the second parameter is the parameter array
}
alert(callSum1(10,10)); //20
alert(callSum2(10,10)); //20

Copy code The code is as follows:

//Change function scope
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 labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template