JavaScript Learning Summary (15)--function class

Source: Internet
Author: User

In JavaScript, a function is actually an object, each function is an instance of a function class, and since it has its own properties and methods, the function name is actually a pointer to a function object and is not bound to a function.

Declaration of a function

Way one: Conventional way

function sum1 (num1,num2) {2      return num1+3}   

Mode two: function expression

var sum2=function(num1,num2) {2      return num1+3};    

Method Three: Dynamically create functions (this is not used much)

var sum3=new function ("Num1", "num2", "return num1+num2");//dynamic creation of functions

Test:

1 <script type= "Text/javascript" >2functionSUM1 (num1,num2) {3Return num1+Num24}5var sum2=function(NUM1,NUM2) {6Return num1+num2;7 }; 8 var sum3=new Function ("Num1", "num2", "Return num1+num2"), 9 // test document.write (" <pre> "); Document.writeln ("sum1" = "+sum1");  Document.writeln ("sum2" = "+sum2");  Document.writeln ("sum3" = "+sum3");  document.write ("</pre>");  </script>                

Test results:

  

By the way, here's the difference between the two ways of declaring a function, and for a function that takes the form of a declaration, the parser takes the lead in reading the function declaration and putting it before any code, and for a function expression, you must wait until the parser executes to the line of code where it is located before executing.

You can use the following code to prove that:

 1 alert ("sum1" = "+sum1")   can run 2 function Sum1 (num1,num2) {3 return num1+< Span style= "COLOR: #000000" >num2 4 }5 alert (" sum2 "=" +sum2 "); // error: Missing object 6 var sum2=function (num1,num2) {7 return num1+NUM2; 8};               
Second, function overloading

JavaScript does not have a method overload, if the two method name, even if the number of arguments is different, then the definition will overwrite the previous definition, call the method is always called after the definition of the.

Use the following code to prove that JavaScript does not support overloading of functions:

1 <script type= "Text/javascript" >2functionTest (a) {3alert (a);4} 5 function Test (A, b) { Span style= "COLOR: #008080" > 6 alert ("helloworld!"  7 } 8 function Test (A, b) { 9 alert (A +" "+b); 10 }11 12 Test (20); // called the last definition of the test method 13 Test (30,50); // called the last definition of the test method 14 </script>              

  把函数名想像成指针,这例子中声明了三个同名函数,最后声明的一个就会覆盖了前面函数,用下面的写法或许更好理解:

1/*Declares a variable test,test a pointer (reference) to a function object in a variable*/2var Test =function(a) {3alert (a);4} 5  6 /* points the variable test to another function object */  7 Test = function  (A, b) { Span style= "COLOR: #008080" > 8 alert ("helloworld!"  9 }10 /* points the variable test to another function object */ 11 Test = function  (A, b) { Span style= "COLOR: #008080" >12 alert (A + "" +b);                  
Third, the particularity of JavaScript function

The function name in JavaScript itself is a variable, so the function can also be used as a normal variable. That is, not only can you pass a function to another function like passing parameters, but you can return a function as the result of another function.

For example, when a constructor is used to initialize a property to an object, the specified function property

1 <script type= "Text/javascript" >2functionFN (Num1,num2,oper) {3var result=-1;4Switch(Oper) {5Case "+":6 result=num1+num2;7Break;8Case "-":9 result=num1-num2;10Break;11Case "*":Result=num1*num2;13Break;14Case "/":result=num1/num2;16Break;17}18ReturnResult19}20/*Create Person class*/21stfunctionPerson (NAME,AGE,FN) {22This. Name=Name23this. Age=age; this. FN=FN; // Use the FN function passed in to initialize the Function property fn  }26 /* When you use a constructor to initialize a property to an object, you can also specify a function property */27 var p1 = new person ("aloof pale Wolf", 24,FN); 28 var retval=p1. Fn ("+"  alert (retVal)  </script>              

Inside the function, there are two special objects: arguments and this,arguments are an array object that contains all the parameters passed in, and the main function of arguments is to save the function arguments, but this object also has a property called callee. This property is a pointer to the function that owns the arguments object.

1//A very classical recursive function2functionFactoriak (num) {3if (num<=1){4Return 1;5}Else{6Return num * facaorial (NUM-1);//Coupling with function name Factoriak is too high.7}8} 9 // The above code is too coupled with the name of the function, If you change the function name, you can use the following method 10 function  Factoriak (num) {11 if (num <=1) {12 return 1; 13} else{14 return num * Arguments.callee (NUM-1); // So no matter what the name can be done recursively call 15 Span style= "COLOR: #000000" >}16}        

This refers to the scope at which the function executes

Each function contains: length and prototype. The Length property represents the number of arguments the function expects to receive.

1functionSayname (name) {2alert (name);3}4functionSUM (num1,num2) { 5 return num1+num2; 6 } 7 function Sayhi () { 8 alert ( "Hi"  9 }10 alert (" The number of parameters received by the Sayname function is: "+sayname.length); //111 alert (" The SUM function receives the number of arguments: "+sum.length"; //212 alert (" The number of parameters received by the Sayhi function is: "+sayhi.length); //0         

Each function contains two non-inherited methods: Apply () and call (); The purpose of both methods is to invoke the function in a specific scope, which is actually equal to setting the value of the This object in the function body

The Apply () method receives two parameters: one is the scope in which the function is run, and the other is the parameter array. The second parameter can be an instance of an array, or it can be an arguments object.

1functionSUM (num1,num2) {2Return num1+num2;3}4functionCallsum (num1,num2) {5return sum.apply (this,arguments); // incoming aguments object  6 } 7 function callSum2 (num1,num2) { 8 return sum.apply (this,[num1,num2]); // incoming array  9 }10 alert (Callsum (10,10)); //2011 alert (callSum2 ( 10,10)); //20         

The call () method takes the first parameter as the scope of the function's run, and the remaining parameters are the parameters (one or more) that are passed to the function when it is run.

function sum (num1,num2) {    return num1+num2;} functionreturn Sum.call (this, num1,num2);} Alert (Callsum (10,10));      

The most powerful place to apply () and call () is the ability to extend the scope in which functions are run

1 window.color= "Red";2var o ={color: "Blue" }; 3 function Saycolor () {4 Alert (this.color);  5 }6 saycolor (); //red7 Saycolor.call ( Span style= "COLOR: #0000ff" >this); //red8 Saycolor.call ( window); //red9 saycolor.call (o); //blue         

JavaScript Learning Summary (15)--function class

Related Article

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.