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

Source: Internet
Author: User
Tags function definition

1 // Three ways to create a function
2 function one(){ // Function declaration, does not belong to any object, always defaults to global object
3 console.log("first function")
4 //The default is a return this, return the contents of the function
5 }
6 one (); / / must be called; can be called before the function declaration (preprocessing mutation mechanism)
7
8 var fn=function(){ //function expression
9 console.log("second function")
10 }
11 fn (); / / must be declared and then called
12
13 var fun=new Function(console.log("third function")); // constructor


1 // 4 ways to call the function
  2 function one(){
  3 console.log("a function")
  4 }
  5 one (); / / as a function to call
  6
  7
  8 var person={
  9 name: "tom",
10 age: 18,
11 speak: function(){
12 console.log("English")
13 }
14 }
15 person.speak(); // function as a method call to the object
16
17
18 function num(n1,n2){
19 this.number1=n1;
20 this.number2=n2;// This has no value in the constructor
twenty one }
twenty two 
23 var i=new num(3,5);
24 console.log(i.number1) // The constructor creates a new object, and the new object inherits the properties and methods of the constructor.
25
26
27 function myFunction(a,b){
28 return a+b;
29 }
30 myFunction.call(this,2,5);
31
32 var myArry=[2,5];
33 myFunction.apply(this, myArry); //call function as a function method call() and apply() are predefined function methods, apply is an array, call is passed in the argument


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

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.