1. Definition of functions

1.1. Mode 1 Function declaration mode Function Keyword (named function)

function fn(){}
Copy the code

1.2. Mode 2 Function Expression (anonymous function)

var fn = function(){}
Copy the code

1.3. mode 3 New Function()

Var fn = new Function(' 1',' 2'... , 'function body ')Copy the code
  • All arguments in Function must be strings

  • The third method is less efficient to implement and is not easy to write, so it is less used

  • All functions are instances of Function

  • Functions are also objects

    var f = new Function('a', 'b', 'console.log(a + b)'); f(1, 2); Var fn = new Function(' 1',' 2'... (' Function body ') note that all arguments in /*Function must be strings. The third method is inefficient and inconvenient to write, so it is rarely used. All functions are instances of Function (objects)Copy the code

2. How to call functions

  1. Common function
  2. Object methods
  3. The constructor
  4. Bind event function
  5. Timer function
  6. Execute function immediately
*/ function fn() {console.log(' peak of life '); } fn(); SayHi: function() {console.log(' the peak of my life '); } } o.sayHi(); */ function Star() {}; new Star(); */ btn.onclick = function() {}; /* 5. Timer function */ setInterval(function() {}, 1000); */ (function() {console.log(' peak of life '); }) ();Copy the code