JS-defined functions There are three ways of Deng Han

Defined functions There are three ways of Deng Han

Naming function definition

Anonymous function definitions

The constructor is defined

Deng Hanzhi function can be placed in any part of the site but before execution of the basic formatting functions functions of the head is <script> function name (parameter) {Code} </ script> name can easily play, but the beginning must be an English letter parameters can be saved directly brackets can also be behind the increase in function spaces Deng Han's name without adding the function calls should be behind bars

function function name (var1, var2, ..., varX ) { code. . . }
Function function name (var1, var2, ..., varX ) { code. . . }
Var1, var2 etc., means that a variable or a value passed to the function. {}, And define the beginning and end of the function.
Note: no argument function must be added in parentheses after the function name:
function name of the function () {code. . . }
Function name of the function () {code. . . }
Note: Do not forget the importance of JavaScript in uppercase and lowercase letters. The word function must be lowercase, or JavaScript error occurs. Also note that you must use exactly the same case the function name to call the function.

< script >

// Here is xyz function can not be performed because the following script has not been created, this function has not xyz on the heap

< /script >

< script >

// function script tag here once created, all following script tag can use this function

// When executing the current script tag, in advance of the current script tag all the named function previously stored on the heap, and
give him the definition of the function name // stack reference the function address

// function name

// Code

function xyz(){

vonsole.log(“aaa”);

}

var fn = xyz; // xyz is the name of the function

xyz (); // perform functions

//fn();

// anonymous function

// anonymous function without a name, you can set a variable to a property of an object or set to

// function of the difference between naming and

// named function can be performed before or after the defined function definition, but the anonymous function can only be performed after the function definition

// define anonymous functions in three ways

// 1) variables anonymous function

fn();

// When run to the line, only to Deng Han of this anonymous function assigned to the variable fn

was fn = function () {

console.log(“bbb”);

}

2 //) method object definition

Var obj = {

fn:function(){

}

}

// 3 self-executing anonymous function

// Disadvantages: can only be executed once, can not be called again

(function(){

console.log(“aa”);

})

// constructor creates

// constructor, the content is in front of all the parameters of this function is created,

// content is the last function block execution sentence

// requires that all content must be strings

// shortcomings, the constructor function to create a disadvantage is running slow, inefficient,

// all because of the need to convert a string of code

// code structure

//var fn=New function(“a”,“b”,”console.log(a+b)”);

function(a,b){

console.log(a+b);

}

< /script >

Guess you like

Origin www.cnblogs.com/blogst/p/12598534.html