In JavaScript, functions exist as first-class members, so it is very necessary to master the knowledge of functions in JavaScript, and in recent days read the third and fourth chapters of the JavaScript Ninja and make a summary.
JavaScript function declaration:
JavaScript functions are declared using function literals to create functions.
Shaped like
Function Name (ARG1,ARG2)//The name of the function is optional {code;};
Scope of the function:
There are no block-level scopes in JavaScript, only function scopes. That is, in JavaScript, scopes are declared by function, not by code blocks.
{ var a=10;} Console.log (a);//The result is ten
function A () { var a=10;} Console.log (a);//The result is undefined
Function call:
1. Calling as a function
function A () {};a ();
2. Calling as a method
var o={};o.haha=function(); haha ();
3. Call as a constructor
function Pig () {}; var xiaohong=New Pig (); var xiaoming=New Pig ();
4. Using the Apply and call method calls
function haha () {}; var hahaha1={}; var hahaha2={};haha.apply (hahaha1,[1,2,3,4]); // The first argument of the apply method is the object of the function context, the second argument is the array haha.call (hahaha2,1,2,3,4) to pass in the parameter, the first argument of the call method is the object of the function context, and the remaining arguments are the arguments to pass in
JavaScript Ninja reading essay (i): function declaration, invocation