Good programmer: Functions in Js for Web testing. In Js, functions are generally used to encapsulate certain operations, or to write programs for modular operations.

1. Declaration of functions

1. Plain function declarations

function box(num1, num2) {

return num1+ num2;

}

2. Use variables to initialize functions

var box= function(num1, num2) {

return num1 + num2;

};

3. Use the Function constructor

var box= new Function(‘num1’, ‘num2′ ,’return num1 + num2’);

Two. As a function of values

Function names in ECMAScript are themselves variables, so functions can also be used as values. That is, not only can one function be passed to another as an argument, but one function can be returned as the result of another function.

function box(sumFunction, num) {

return sumFunction(num);

}

function sum(num) {

return num + 10;

}

var result = box(sum, 10);

3. Function internal attributes

Inside the function, there are two special objects: Arguments and this. Arguments is a class array object that holds all the arguments from the function passed in. Its main purpose is to hold function arguments. But this object also has a property called Callee, which is a pointer to the function that owns the Arguments object.

function box(num) {

if (num <= 1) {

return 1;

} else {

return num * box(num-1);

}

}

For factorial functions, you usually use recursive algorithms, so the function must call itself; There is no problem if the function name is not changed, but once the function name is changed, the internal call itself needs to be modified one by one. To solve this problem, we can use arguments.callee instead.

function box(num) {

if (num <= 1) {

return 1;

} else {

return num * arguments.callee(num-1);

}

}

Another special object inside the function is this, which behaves roughly like this in Java and C#. In other words, this refers to the object on which the function operates, or the scope in which the function calls the statement. PS: When a function is called in the global scope, this refers to the window.

Window. color = ‘red ‘;

alert(this.color);

var box = {

Color: blue

sayColor : function () {

alert(this.color);

}

};

box.sayColor();

alert(this.color);

Function attributes and methods

Functions in ECMAScript are objects, so functions also have properties and methods. Each function contains two attributes: Length and Prototype. Where, the length attribute represents the number of named parameters that the function wants to receive.

function box(name, age) {

alert(name + age);

}

alert(box.length);

function box(num1, num2) {

return num1 + num2;

}

function sayBox(num1, num2) {

return box.apply(this, [num1, num2]); }

function sayBox2(num1, num2) {

return box.apply(this, arguments);

}

Alert (sayBox (10, 10));

Alert (sayBox2 (10, 10));

The call() method is the same as the apply() method; they differ only in the way they receive arguments. For the call() method, the first argument is scope and there is no change, except that the rest of the arguments are passed directly to the function.

function box(num1, num2) {

return num1 + num2;

}

function callBox(num1, num2) {

return box.call(this, num1, num2);

}

Alert (callBox (10, 10));

Var color = ‘red ‘;

var box = {

Color: ‘blue’

};

function sayColor() {

alert(this.color);

}

sayColor();

sayColor.call(this);

sayColor.call(window);

sayColor.call(box);

When we use the call(box) method, the sayColor() method runs in a box object.

The biggest benefit of using call() or apply() to extend the scope is that objects do not need to be coupled to methods in any way, and extension and maintenance have a chain reaction.