In many languages, functions (which are methods in Java) and objects differ in two different things. A function is defined as the action of an object, or it is global (like the main function in C + +). But in JavaScript, the boundaries between functions and objects seem less obvious.
1. Definition of function
There are many ways to define functions in javascript:
function hello() { alert("Hello!"); }
var hello1 = function() { alert("Hello! "); };
var hello2 = new Function("", "alert('Hello! ');");
hello();
hello1();
hello2();
Three JavaScript function definition statements are given above. The first sentence is a common definition that doesn't look much different from languages like Java. This sentence defines a named function, which, according to the example above, defines the name hello. The second sentence is to define an anonymous function and assign it to a variable, so you can refer to the anonymous function by using this variable. These two sentences look the same, but they are different: the first sentence defines a named function, and the second sentence defines an anonymous function--although you can refer to the anonymous function through this variable, it is actually anonymous. The difference can be seen from the following:
hello();
hello1(); // error
function hello() { alert("Hello! "); }
var hello1 = function() { alert("Hello!"); };
The scope of the named function is global: You can use this function before you define it. However, the definition of an anonymous function is a back-end, as in C + +, which must be defined before it can be used. This is why hello can be used, but hello1 will have errors. And think about what this is for? JavaScript's interpretation process is the same as HTML from top to bottom. Therefore, the anonymous function here is equivalent to the definition of a variable, so there is an error when the JavaScript interpreter interprets execution without knowing the definition of the variable. However, the definition of a function is to scan the global.
The third statement is interesting. It creates an object of a function class. This constructor (let's call it) has two parameters, the first is the parameter of the function, and the second is the function body. Specifically, the following two function definitions are equivalent:
function sayHelloTo(name) {
alert("Hello, " + name);
}
var sayHelloTo1 = new Function("name", "alert('Hello, ' + name)");
This is not a common way to define a function, but this statement shows an interesting feature: it means that you can use this constructor to dynamically construct the constructor at run time! This is not the characteristic of the general language.
2. Parameters of function
The function of JavaScript is also quite flexible, not only is it defined in a variety of ways, even its parameters have "strange" behavior. Because JavaScript is a weakly typed language, it cannot detect your function parameter types, and it doesn't even guarantee that the number of arguments you pass in is the same as the function definition. This requires some special testing.
function sum2(a, b) {
alert(a + b);
}
sum2(1); // NaN
sum2(1, 2); // 3
sum2(1, 3, 5); // 4
To look at this example, a function that accepts only two parameters can have any arguments when invoked! However, it takes only the number of eligible items, and here is the first two parameters. So, when you pass in a parameter, JavaScript tries to add up to two digits, and the second argument does not exist, so the return value is Nan. In the third case, the number of arguments is more than the number of formal parameters, at which point JavaScript adds only the first two parameters.
Although it is not formal, it can be said that the function parameters of JavaScript are indefinite parameters, that is, you can pass in any parameter values. Arguments, which is built with JavaScript functions, can traverse all incoming parameters. For example, the following code:
function sum() {
var total = 0;
for(var i = 0; i < arguments.length; i++) {
total += arguments[i];
}
alert(total);
}
sum(1, 2);
sum(1, 2, 3);