JavaScript function Type Introduction _javascript tips

Source: Internet
Author: User

In JS, the function type is actually an object, each function is an instance of a function type, and has properties and methods as well as other reference types;

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

How to declare a function

1. Function declaration method Functions
  Box (num1,num2) {return
    num1+num2;
  }

2. Function expression definition function
  var box = function (num1,num2) {//variable box can be used to reference functions;
    return num1+num2;
  };  Note that there is a semicolon at the end of the function, just as when declaring other variables;
var another = box;    The function name with no parentheses is the access function pointer, not the calling function;
Console.log (another (10,10));
3. Use function constructor
  var box = new function (' Num1 ', ' num2 ', ' return num1+num2 ');
The third way is not recommended, this syntax will lead to parsing two times code (the first time to parse the regular JS code, the second resolution of the string in the constructor), which affect performance;
This syntax can be used to understand the concept of "function is object, function name is pointer";

Function of two as values

The function name in JS itself is a variable, so the function can also be used as a value;
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;
  function box (sumfunction,num) {//No matter what functions the first argument passes in, return
    sumfunction (num); It returns the result after the first parameter has been executed
  ;
  function sum (num) {return
    num+10;
  }
  Transfer function to another function;
To access the function of the pointer does not execute the function, you need to remove the parentheses after the function name;
  var result = box (sum,10);      =>20;

Three function Internal properties

There are two special objects inside the function: arguments and this;

1.arguments: is a class array object, contains all the parameters in the incoming function, the main purpose is to save the function parameters;
Arguments This object also has a property called Callee, which is a pointer to the function that owns the arguments object;
  function box (num) {
    if (num<=1) {return
      1;
    } else{return
      Num*arguments.callee (num-1);  Use Arguments.callee to execute box itself;
    }

2.this: Refers to the object to which the function is manipulated, or the scope of the function call statement;
When a function is called in the global scope, the This object refers to window;
  Window.color = "Red";
  alert (this.color);        Print the global color;=>red;
  var box = {
    color: ' Blue ',
    saycolor:function () {
      alert (this.color);    Print local color;=>blue;
    }
  };

Four function properties and methods

The functions in JS are objects, so functions also have attributes and methods, including length and prototype;
  Length property: Represents the number of named arguments that the function expects to receive;
  function box (name,age) {alert (name+age);        alert (box.length);
2s//Prototype property: It is the true location of all instance methods, that is, the prototype;
The prototype contains two methods: Apply () and call (), and each function contains the two inheritable methods;
  The purpose of both methods is to call a function in a particular scope, which is actually equal to the value of the This object in the function body.
  var color = ' red ';
  var box = {color = ' blue ';
  Function Saycolor ({alert (this.color);
  });           Saycolor ();
  Scope in window;      Saycolor.call (this);
  Scope in window;     Saycolor.call (window);
  Scope in window;       Saycolor.call (box);
Scope in box, object posing as;=>red;
When using the call (box) method, the Saycolor () method's operating environment has become a box object;
The greatest benefit of using call () or apply () to augment the scope is that the object does not need to have any coupling relationship with the method;
Coupling: The meaning of correlation, the extension and maintenance will have a ripple effect;

  In other words, the box object and the Saycolor () method will not have redundant associated operations, such as: Box.saycolor = Saycolor;  
    function Animal () {this.name = "Animal";  
    This.showname = function () {alert (this.name);  
  } function Cat () {this.name = "cat";  
  var animal = new animal (); VaR cat = new Cat ();  
  Using the call or Apply method, the ShowName () method originally belonging to the animal object is given to Object cat.  
  The input result is "cat" Animal.showName.call (Cat, ","); Animal.showName.apply (cat,[]);

The

Five summary
1//function is actually an instance of the function type, so the functions are also objects; This is where the formal JavaScript is most distinctive;
2//Due to function objects, so functions also have methods, can be used to enhance its behavior;

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.