Definition of a function
Function names can contain only letters, numbers, underscores, or $, and cannot begin with a number. When defined, a function definition expression or function declaration statement is available.
var f = function fact (x) {}
The function definition expression contains the name, which is used as the local variable of the function inside the function, and the surrogate function.
A function declaration statement is not a true statement and cannot appear in loops, conditions, try/catch/finally, and with statements, where a declaration statement is placed where it will not be executed, can still be accessed by the entire scope, and can be used before the code is defined. The variable declaration that defines the expression is advanced, but the assignment to the variable does not advance, the function cannot be used before it is defined, or an error occurs when called: "typeerror:undefined is not a function"
The return statement does not have an expression associated with it, it returns the undefined value, and if a function does not contain a return statement, it executes only the function body statement and returns the undefined to the caller; a function that has no return value, sometimes called a procedure.
function execution
Functions are performed in several ways, as functions, methods, constructors, and indirect calls. If the constructor has no formal parameters, the argument list and parentheses can be omitted, and null or undefined placeholders may be used instead of blank parameters when calling function arguments with intervals.
functionMyClass (name) { This. Name =name; returnName//The return value of the constructor function?}varObj1 =NewMyClass (' foo ');//MyClass ObjectvarObj2 = MyClass (' foo ');//' foo 'varObj3 =NewMyClass ({});// {}varObj4 = MyClass ({});// {}Argument Object
The argument object is a class array object, which can be called recursively, and if you name the parameter arguments, then this parameter will overwrite its original special variable Arguments.callee.
Formal parameters and arguments for a function
The defined function in parentheses does not pass in the corresponding argument, and the default value is undefined, which is used implicitly to define local variables inside the function. The checksum of the passed-in parameter throws an error, and the arguments in the function pass in the reference, inside the function for its operation, externally visible.
// function Parameter Reference var oo = { 1, y:2, get Z () { return 3;} } function FNS (obj) { = 4;} FNS (OO);
function Properties, methods
Functions are also objects, so there are properties and methods, the length property of the function, and the number of function parameters.
The second argument to the Apply method can be an array or a class array object.
The Bind method is new in ES5, binds the function to the object and passes in a subset of the arguments, and the passed-in arguments are placed to the left of the full argument list.
/** * ES5.0 support bind function, ES3.0 does not support bind, here in ES3 simulation implementation, can be applied to most scenes, * if there is a problem, welcome to explore together * https://developer.mozilla.org/en/ Javascript/reference/global_objects/function/bind*/Function.prototype.bind|| (Function.prototype.bind =function(that) {vartarget = This; //If iscallable (func) is false, throw a TypeError exception. //validating the incoming context when called by call, apply if(typeofTarget!== ' function ') { Throw NewTypeError (' Bind must is called on a function '); } varBoundargs = Slice.call (arguments, 1); functionbound () {//The returned bind function is called when the constructor if( This instanceofbound) { varSelf =CreateObject (Target.prototype); varresult =target.apply (Self, boundargs.concat (slice.call (arguments))); //Object (Result) = = = Result Determines whether the call returned is an object returnObject (Result) = = = result?result:self; } //The returned bind function is called as a general function Else { returntarget.apply (that, Boundargs.concat (slice.call (arguments))); } } //notice:the Function.length is not writable.Bound.length = Math.max (target.length-boundargs.length, 0); returnbound;});Higher order functions
If a function is used as a parameter or as a return value, it is called a higher order function.
Functions as namespaces
There are several ways to expose the interface in a function namespace
var mylib = (function (global) { function log (msg) { console.log (msg) ; } = log; // law one: Using the default behavior of variable declarations without Var, the log1 becomes a global variable (not recommended) global.log2 = log; // method Two: Add the Log2 property directly on the global object and assign the value to the log function (recommended) return { // Law III: Returns the value of a series of interface functions through an anonymous function set object, assigned to global variable Mylib (recommended) log:log };} (window));
Self-updating functions
function selfupdate () { function () { alert (' second run! ') ); }; Alert (' first run! ' // first run! // Second run!
This function can be used to run logic only once, after the first run the whole is replaced by a new logic.
function Writing Specification
Several comments on function parameter definition
/*,...*/Ap.reduce|| (Ap.reduce =function*f,/*, initial*/) {getpropertynames (O,/*Optional*/A///*optional*/indicates that the parameter is optional functionArraycopy/*Array*/From/*Index*/From_start) {};}Functions with memory function
/* * * memory function function memorize (f) { var cache = {}; return function () { var key = arguments.length + "+ Array.prototype.join.call (Argu ments, ', ' ); if (! ( Key in cache)) Cache[key] = f.apply ( Span style= "color: #0000ff;" >null return Cache[key]; }}
Summary of Javascript functions