Javascript Reading Notes: function definitions and function calls

Source: Internet
Author: User

Define a function using the function keyword to define a function. There are two forms: declarative function definition; function add (m, n) {alert (m + n );} this method is equivalent to constructing a Function class instance: var add = new Function ("m", "n", "alert (m + n );"); the last parameter of the Function constructor is the Function body: "alert (m + n);". The preceding parameters are all Function parameters. The parameters must be in the string format: "m ", "n ". Function expression; copy code 1 // assign an anonymous function to the variable add 2 var add = function (m, n) {3 alert (m + n); 4 }; 5 add (1, 2); // 3 6 7 // The function expression is passed as a parameter to other functions 8 var arr = [1, 3, 4, 2]; 9 arr = arr. sort (function (a, B) {10 return a-B; 11}); 12 alert (arr); // 1, 2, 3,413 14 // call 15 alert (function (m, n) {16 return m + n; 17} (1, 2) immediately after definition )); 18 19 // a function expression can also define a name (for example, for recursion) 20 var f = function fact (x) {21 if (x <1) {22 return 1; 23} else {24 return x * fact (x-1 ); 25} 26}; copy a code declarative function definition and a function expression. The declarative Function Definition Statement falls within the top-level scope. The function call statement can be written before the function declaration statement: add (1, 2); // 3 function add (m, n) {alert (m + n);} the function defined by the function expression cannot be called before it is defined: // TypeError: add is not a functionadd (1, 2); var add = function (m, n) {alert (m + n );}; the return value of a function in Javascript does not need to declare the return type. The return keyword is used to return a value. If a function does not contain a return statement or directly uses "return;", the return value of the function is undefined. Nested function functions can be nested in other functions. The inner function can access parameters and variables of the outer function: copy the code function add (m, n) {function d (x) {return x * 2;} return d (m) + d (n);} alert (add (1, 2 )); // 6 copy the code and call the function body code. The code is executed only when the function is called. There are 4 call methods in Javascript Functions: common function call method call constructor call the most commonly used function call methods through call () and apply (), such: alert ("hello"); var result = add (1, 2); a method call is called to assign a function to an object's attribute: copy code 1 // define a function 2 function hello (name) {3 alert ('hello, '+ name); 4}; 5 var user = {}; 6 7 // The sayHi attribute assigned to the user is 8 users. sayHi = hello; 9 10 // method call 11 user. sayHi ('zhang san'); one difference between common function calls and method calls by copying code: In the "common function call" method, the context of the function call (the value of this ), it is a global object (non-strict mode) or undefined (strict mode ). In the "method call" method, this points to the current object. With this feature, we can directly return this when the method does not require a clear return value, thus implementing the "method chain ". For example, '{btn_edit'{.css ({color: red}), which is common in jquery }). show (); in nested functions, this: in nested functions, the inner function does not inherit this of the outer function. That is, when the inner function is called as a method, this of the inner function points to the currently called object; when an inner function is called as a function, the value of this is a global object (non-strict mode) or undefined (strict mode ). How to access this of the outer function in the internal function? This is usually stored in a local variable and accessed through the variable: copy the Code 1 var obj = {2 f: function () {3 var self = this; 4 console. log (this = obj); // true, this points to the current object 5 6 f1 (); 7 8 function f1 () {9 console. log (this = obj); // false, this is a global object or unde10 console. log (self = obj); // true, self points to the outer layer this, that is, the current object 11} 12} 13 }; when the new keyword is used to create an object, the constructor is called. If the constructor does not have any form parameter, You can omit the parentheses: var obj = new Object (); // equivalent to var obj = new Object; call the constructor and create a new Object, the new object will become the call context of the constructor (value of this): function User (name) {this. name = name; console. debug (this);} var user = new User ('zhang san'); call () and apply () Indirect calls to functions in Javascript are also objects and methods. Call () and apply () can be used to call functions indirectly. The first parameter of call () is used to specify the call context (that is, the value of this). The following parameter is the real parameter passed in to the call function. Copy code 1 var name = 'a'; 2 var user = {3 name: 'B' 4}; 5 6 function showName () {7 alert (this. name); 8} 9 10 showName (); // A, this is the Global Object 11 showName. call (user); // B, this is the user object copy code. apply () and call () are similar, the difference is that, the following arguments must be passed in the form of an array (the arguments array of the current function can be passed in directly ).

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.