Functions, function declarations and expressions, function calls

Function syntax A

function is a block of code wrapped in curly braces. The keyword function is used in front of it:

function functionname()

{

here is the code to be executed

}

When the function is called, the code in the function will be executed.



Function declaration and function expression

function arr(){}: //declaration, because it is part of the program

var bar=function bar(){}; //expression, because it is part of an assignment expression

new function bar(){}; //expression Formula, because it is a new expression

(function(){ //declaration, because it is part of the function body

})(); four methods of

function call

js function call: method call mode, function call mode, constructor call Mode, apply, call call mode

1, method call mode:

first define an object, and then define the method in the object's properties, execute the method through myobject.property, this refers to the current myobject
object.
var blogInfo={
  blogId:123,
  blogName:"werwr",
  showBlog:function(){alert(this.blogId);}
};

blogInfo.showBlog(); //123

2. Function calling mode

Define a function, set a variable name to save the function, then this points to the window object.

var myfunc = function(a,b){
  return a+b;
}

alert(myfunc(3,4)); // 7

3. Constructor calling mode

Define a function object, define properties in the object, and define properties in its prototype object method defined in . When using a prototype's methods, the object must be instantiated in order to call its methods.
var myfunc = function(a){
  this.a = a;
};
myfunc.prototype = {
  show:function(){alert(this.a);}
}

var newfunc = new myfunc("123123123");
newfunc.show (); // 123123123

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326628272&siteId=291194637