JavaScript function methods

Source: Internet
Author: User

Function 1. Definition of functions (1) Function function name (x) {function execution body;} (2) var functions name =function (x) {function execution body;}; This method shows that in JavaScript, a function is an object, that is, a function is a data type, and a parameter list is equivalent to the entry of a function, and return is equivalent to the exit of a function. Example: var abs = function (x) {    if (x >= 0) {        return x;   } else {        return-x;   }};function (x) {...} is an anonymous function that does not have a function name. However, this anonymous function assigns a value to the variable abs, so the function can be called by using the variable abs. The two definitions are completely equivalent, and note that the second way is to add one at the end of the function body according to the complete syntax, which means that the assignment statement ends. 2. Function call because JavaScript allows you to pass in any parameter without affecting the call, there is no problem in passing in parameters that are more than the defined parameters, although they are not required inside the function. ABS (, ' Blablabla '); Return 103.argumentsJavaScript There is also a free keyword, arguments, which works only inside the function and always points to all arguments passed in by the caller of the current function. Arguments resembles an array but it is not a array:function foo (x) {    alert (x);//10    for (var i=0; I<arguments.leng Th i++) {        alert (arguments[i]);//ten, 30   }}foo (10, 20, 30); Actually arguments is most commonly used to determine incoming parameters The number of. You may see this writing://foo (a[, b], C)//To receive two or three parameters, B is an optional parameter, if only 2 parameters are passed, b defaults to Null:function foo (A, B, c) {    if (Arguments.leng th = = 2) {  &nbsP    //The actual parameters obtained are a and b,c for undefined        C = b; Assign B to c        B = null; B becomes default    }   //...} To change the intermediate parameter B to an "optional" parameter, you can only judge by arguments and then re-adjust the parameter and assign a value. 4.restfunction Foo (A, B, ... rest) {    Console.log (' a = ' + a);    Console.log (' B = ' + b);  &nbs P Console.log (rest);}  foo (1, 2, 3, 4, 5);//Result://a = 1//b = 2//Array [3, 4, 5] foo (1);//Result:/a = 1//B = undefined//Array [] From The result of the operation shows that the parameters passed in first bind a, B, and the extra parameters are given to the variable rest in the array form, so we get all the parameters without arguments. It does not matter if the passed parameters are not filled with the normal defined parameters, and the rest parameter receives an empty array (note that it is not undefined). Method 1. Bind a function in an object, called the method of this object. Example: var xiaoming = {    Name: ' xiaoming ',    birth:1990,    age:function () {        var y = new Date (). getFullYear ();        return y-this.birth;   }};xiaoming.age; function Xiaoming.age () xiaoming.age (); This year the call is 25, and next year the call becomes 26.2.this inside a method, this is a special variable that always points to the current object, that is, the xiaoming variable. So, This.birth can takeThe birth property to Xiaoming. If disassembled: function Getage () {    var y = new Date (). getFullYear ();    return Y-this.birth;}  var xiaoming = {    Name: ' xiaoming ',    birth:1990,    age:getage}; xiaoming.age (); 25, normal result getage (); nan  if the function getage () is called separately, this point of the function points to the global object, which is window. 3.apply Although in a separate function call, depending on whether it is strict mode, this point points to undefined or window, however, we can still control the point of this! To specify which object this function is pointing to, you can use the Apply method of the function itself, which receives two parameters, the first parameter is the this variable that needs to be bound, and the second parameter is an array that represents the parameters of the function itself. Fix Getage () call with apply: function Getage () {    var y = new Date (). getFullYear ();    return Y-this.birth;}  var xiaoming = {    Name: ' xiaoming ',    birth:1990,    age:getage}; xiaoming.age (); 25getage.apply (Xiaoming, []); The this point xiaoming, the parameter is empty another method similar to apply () is call (), the only difference being: Apply () packages the parameters into an array and then incoming; call () passes the parameters sequentially.

JavaScript function Methods

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.