Javascript--function Type (11)

Source: Internet
Author: User

//In JS, the function type is actually Object each function is of type Example , and all the same as other reference types has properties and methods ;

Since the function is an object, the function name is actually a pointer to the function object ;

How a function is declared
11. General Declaration Method2     function Box(num1,num2) {3         returnnum1+num2;4     }5 62. Using variable initialization functions7     var box = function (num1,num2) {8         returnnum1+num2;9     }Ten  One3. Using the function constructor A     var box = new Function(' Num1 ', ' num2 ', ' return num1+num2 '); - //The Third Way is not recommended, this syntax will lead to parsing two times the code (the first parsing the regular JS code, the second parse the string passed into the constructor), thereby affecting performance; - //This syntax can be used to understand the concept of "function is object, function name is pointer";
Two functions as values
1 //the function name in JS itself is a variable , so the function can also be used as a value ;2 //that is, not only can you pass a function to another function like a parameter, but you can return a function as the result of another function;3     functionBox (sumfunction,num) {4         returnsumfunction (num);5     }6     functionsum (num) {7         returnNum+10;8     }9     //transfer functions into another function;Ten     varresult = Box (sum,10);//=>20;
Three function Internal properties
1 //There are two special objects within a function:arguments andthis;2 3 //1.arguments: Is a class array object, contains all the parameters passed in the function, the main purpose is to save the function parameters ;4 //arguments This object also has a property called callee , which is a pointer to the function that owns the arguments object;5     functionbox (num) {6         if(num<=1){7             return1;8}Else{9             returnNum*arguments.callee (num-1);//use callee to perform itself; arguments.callee=box;Ten         } One     } A  - //2.this: Refers to the object to which the function is manipulated, or the scope in which the function invocation statement is located; - //when a function is called at a global scope, the This object refers to window; theWindow.color = "Red"; -Alert This. color);//print a global color;=>red; -     varbox = { -Color: ' Blue ', +Saycolor:function(){ -Alert This. color);//print a local color;=>blue; +         } A};
Four function properties and methods
1 //the function in JS is an object, so the function also has properties and methods ; It contains length and prototype;2 3 //Length Property: Indicates the number of named parameters that the function expects to receive ;4     functionBox (name,age) {5Alert (name+Age );6     }7alert (box.length);//2s8 9 //Prototype Property: It is the true location of all instance methods, that is, prototypes ;Ten //prototype contains two methods: Apply () and call (), each containing the two non-inherited methods ; One //The purpose of both methods is to  invoke the function in a specific scope , which is actually equivalent to setting the value of this object in the function body ; A     varcolor = ' Red '; -     varbox = { -color = ' Blue '; the     } -     functionSaycolor ({ -Alert This. color); -     }); +Saycolor ();//scope in window; -Saycolor.call ( This);//scope in window; +Saycolor.call (window);//scope in window; ASaycolor.call (box);// scope in box, object impersonating ;=>red; at //when using the call (box) method, theoperating environment of the Saycolor () method has become a box object ; - //the biggest benefit of using call () or apply () to extend the scope is that the object does not need to have any coupling with the method ; - //coupling: The interrelated meaning, expansion and maintenance will have a chain reaction; - //that is, there is no extraneous operation between the box object and the Saycolor () method, for example: Box.saycolor = Saycolor; -  -     functionAnimal () { in          This. Name = "Animal";  -          This. ShowName =function(){     toAlert This. Name);  +         }     -     }     the     functionCat () { *          This. Name = "Cat";  $     }    Panax Notoginseng     varAnimal =NewAnimal ();  -     varCat =NewCat ();  the     //using the call or Apply method, the ShowName () method that originally belonged to the animal object is given to the object cat .  +     //The input result is "Cat" AAnimal.showName.call (Cat, ",");  the     //animal.showName.apply (cat,[]);

Javascript--function Type (11)

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.