Introduction to JavaScript Function types, javascriptfunction
// In JS, the Function type is actually an object; each Function is an instance of the Function type, and has attributes and methods like other reference types;
// Because the function is an object, the function name is actually a pointer to the function object;
I. function declaration method
1. function declaration method function box (num1, num2) {return num1 + num2;} 2. function expression defines the function var box = function (num1, num2) {// use the variable box to reference the function; return num1 + num2 ;}; // note that there is a semicolon at the end of the function, just like when declaring other variables; var another = box; // use a function name without parentheses to access the function pointer rather than call the function; console. log (another (10, 10); 3. using the Function constructor var box = new Function ('num1', 'num2', 'Return num1 + num2'); // The third method is not recommended, this syntax will cause the code to be parsed twice (the first time the regular JS Code is parsed, and the second time the string passed into the constructor is parsed), thus affecting the performance; // you can use this syntax to understand the concept of "functions are objects and function names are pointers;
Binary Function
// The function name in JS is a variable, so the function can also be used as a value; // That is to say, not only can a function be passed to another function like a parameter, in addition, one function can be returned as the result of another function. function box (sumFunction, num) {// no matter what function is passed in by the first parameter, return sumFunction (num ); // It will return the result after executing the first parameter;} function sum (num) {return num + 10;} // pass the function to another function; // if you want to access the function pointer without executing the function, you must remove the parentheses after the function name; var result = box (sum, 10); // => 20;
Internal properties of the three functions
// The function has two special objects: arguments and this; // 1. arguments: A class array object that contains all the parameters in the input function. It is mainly used to save the function parameters. // arguments also has a property named callee, this property is a pointer pointing to the function that owns this arguments object; function box (num) {if (num <= 1) {return 1 ;}else {return num * arguments. callee (num-1); // use arguments. callee to execute the box itself;} // 2. this: refers to the object in which the function is operated, or the scope of the function call statement. // when a function is called in the global scope, this object references window; window. color = "red"; alert (this. color); // print the global color; => red; var box = {color: 'blue', sayColor: function () {alert (this. color); // print the local color; => blue ;}};
Four Function Attributes and Methods
// Functions in JS are objects, so functions also have attributes and methods, including length and prototype; // length attribute: indicates the number of parameters that the function wants to receive; function box (name, age) {alert (name + age);} alert (box. length); // 2 s // prototype attribute: It stores the true meaning of all the instance methods, that is, the prototype; // prototype contains two methods: apply () and call (). Each function contains these two non-inherited methods. // The purpose of these two methods is to call the function in a specific scope, actually equal to setting the value of this object in the function body; var color = 'red'; var box = {color = 'blue';} function sayColor ({alert (this. color) ;}); sayColor (); // The scope is in window; sayCo Lor. call (this); // The scope is in window; sayColor. call (window); // The scope is window; sayColor. call (box); // The scope is in box, and the object impersonates; => red; // when the call (box) method is used, sayColor () the running environment of the method has become a box object; // The biggest benefit of using call () or apply () to expand the scope is that the object does not need to have any coupling relationship with the method; // coupling: mutual association means that expansion and maintenance will have a chain reaction; // that is, there will be no redundant Association operations between the box object and the sayColor () method, such as: box. sayColor = sayColor; function Animal () {this. name = "Animal"; this. showName = function () {alert (this. name) ;}} func Tion Cat () {this. name = "Cat";} var animal = new Animal (); var cat = new Cat (); // use the call or apply method to convert the showName () that originally belongs to the Animal object () the method is used by the object cat. // The input result is "Cat" animal. showName. call (cat, ","); // animal. showName. apply (cat, []);
Summary
1 // The Function is actually a Function-type instance, so the Function is also an object. This is the most characteristic of JavaScript;
2 // because of the function object, the function also has methods that can be used to enhance its behavior;