Redo JS Virtuoso nineth bullet: function expression

Source: Internet
Author: User

function expressions, what concepts, function expressions in expressions.

1 Function Declaration

The function name ([function parameter]) {

function body

}

In JS, no matter how the display function like this is placed before the call or after the call, it will not affect the use, because the JS interpretation engine will be the function declaration of the advance, which is very well understood;

2 function Expressions

var functionv=function function name ([function parameter]) {

function body

};

A variable in this way points to a function, that's all. JS engine only treats him as a variable of a function type, that's all.

3 Recursive calls

We know that the function name is just a pointer to the function, then the meaning of the function name can point to other variables, it can be null and so on;

function sum (n) {return n+sum (n-1);}

If you use a function expression here, then there's going to be a problem, to see what's going on:

var ansum=sum;

Sum=null;

Ansum (10);//This time the call, the recursion when the internal or use the sum pointer, but, but at this time the sum has been changed, resulting in an exception to the call, which is certainly not what the programmer would like to see the results? What to do

In the function section you already know that functions include several properties, Name,length,arguments,prototype, and so on, where arguments contains a pointer variable callee to a function, which represents the function itself, and callee is read-only, So we should write the recursive code like this.

function sum (n) {return N+arguments.callee (n-1);}

4 Impersonation block-level scopes

JS is easy to create a bunch of hosted in the Window object under the variables, functions, and so on, although JS does not say, but these objects no one asked, he is window in silent management, In a reference to a lot of JS file page inevitably encountered variable function naming conflict caused by the overlap or instability and other inexplicable problems, then you need a mechanism to make JS code modularity, variables, functions are also modular, for the external window this object as little as possible to release some of the global variables, and functions, Especially meaningless. The mechanism here needs to be outside the function of the internal variables, but for the inside of the function is publicly accessible, just like family and public resources.

How to implement modular scope in JS?

(function name () {}) ();///functions that are called automatically, such as declared functions are automatically called, which also has the scope of closed encapsulation, the modular way of writing is actually a closure of the embodiment, its internal non-constructor defined variables and functions are called static variables

and functions

5 static variables and functions

(function () {

Static variables--------accessed through special methods and are shared for instance objects

var a=1;

Static functions-----shared by Instance objects

function Sayhell () {console.log (' 0000)};

Declaring module objects

var module=function () {};

  

Outward exposure through privileged methods

Module.prototype.accessa=function () {return A;};

Module.prototype.accessmethodsayhell=function () {return sayhell;};

})();

6 Private variables and methods

(function name () {

Defined inside the constructor.

Private variables--------accessed through special methods

var a=1;

Private Function-----

function Sayhell () {console.log (' 0000)};

This is called a public variable through this point.

This.name= ";

})();

7 Module Mode

Module mode is also known as a singleton mode, in general, the single case in JS is the use of literal to achieve;

var obj={

Name: ",

Say:function () {console.log (' sssssss ')}

};

However, this does not protect and isolate the internal member Name,say, so these internal private variables should be exposed through privileged methods to control access security;

var singleton=function () {

var name= ';

var say=function () {console.log (' sssssss ')};

  

return {

Name:name,

Say:function () {return Say;}

}

};

This becomes a standard for module mode, because there is only one instance of the object itself, but there is a disadvantage, that is, the object created does not have type use type detection operator: Instanceof also can not detect, in order to solve this problem, proposed the Enhanced module mode.

8 Enhanced Module mode

var singleton=function () {

var name= ';

var say=function () {console.log (' sssssss ')};

  

var obj=new subtype ();//exposes these private members to the specified type so that the returned singleton object has a constructor of a type and can also use instanceof detection. So it is an enhancement;

Obj. Name=name;

Obj. Say=function () {return Say;}

return obj;

};

Redo JS Virtuoso nineth bullet: function expression

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.