Three kinds of creation of JS function, four kinds of call

Source: Internet
Author: User

Tags: Log fun array method execution Environment function create pre parameter

1 //three ways to create a function2 functionOne () {//function declaration, not belonging to any object, always defaults to global object3Console.log ("First function")4 //The default has a return this, which returns the contents of the function5 }6One ();//must be called; can be called before a function declaration (preprocessing mutation mechanism)7 8 varfn=function(){//function Expression9Console.log ("Second function")Ten } OneFN ();//you must first declare and then call A  - varfun=Newfunction (Console.log ("third function"));//The constructor does not need to be called and is self-tuning
1 //4 ways to call functions2 functionOne () {3Console.log ("a function")4 }5One ();//as a function to invoke6 7 8 varperson={9Name: "Tom",TenAge:18, OneSpeakfunction(){ AConsole.log ("中文版")) - } - }                    thePerson.speak ();//the invocation of a function as a method of an object -  -  - functionnum (n1,n2) { +  This. number1=N1; -  This. number2=n2;//This does not have any value in the constructor + } A  at  varI=NewNum (3,5); -Console.log (I.number1)//The constructor creates a new object, and the new object inherits the properties and methods of the constructor -  -  -  functionMyFunction (A, b) { -  returnA +b; in  } -Myfunction.call ( This, 2,5); to  +  varmyarry=[2,5]; -Myfunction.apply ( This, Myarry);//calling functions as function methods call () and apply () is a predefined function method, and apply is an array that is passed in as a parameter

JS operation mechanism PROBLEM: (Declaration of Ascension)
1, JS engine in JS will prioritize the analysis of var variables and function definitions! Step from top to bottom after pre-parsing is complete!
2, when parsing the var variable, the value will be stored in the "execution environment", and will not be assigned value, value is the role of storage! For example:
alert (a); var a = 2; This will output undifiend, meaning that no initialization has been assigned!
This is not undefined, wrong meaning!
3, the function will be parsed when the whole definition, which explains why the function definition functions can be called after the declaration of the first! In fact, on the surface is called first, in fact, in the internal mechanism of the first step is to define functions as a function of the first declaration (preprocessing)

Three kinds of creation of JS function, four kinds of call

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.

Tags Index: