Introduction to JavaScript function types

Source: Internet
Author: User
Tags constructor

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 2 3 4, 5 6 7 8 9 10 11 12 13 14 15 1. Function declaration method Functions Box (NUM1,NUM2) {return num1+num2;}                  2. function expression definition functions 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

1 2 3 4 5 6 7 8 9 10 11 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 is executed;     function sum (num) {return num+10;}//Transfer functions into 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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21-22 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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 This is the object of the


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

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.