JavaScript classes and Modules (ii)

Source: Internet
Author: User

  1. Duck Type: A duck that walks like a duck, swims and is a quack. For Javascript Programmers, this sentence can be interpreted as "if an object can walk, swim, and rattle like a duck, it is considered a duck, even if it is not inherited from the duck's prototype object."

  2. ToJSON() Method: This method is automatically called by Json.stringify () . the JSON format is used for well-serialized data structures and can handle Javascript raw values, arrays, and pure objects. It is independent of the class and ignores the prototype and constructor of an object when it performs a serialization operation on an object.

  3. Using closures to encapsulate States must run slower than equivalent classes that do not use encapsulated state variables and consume more memory

  4. module: Is a stand-alone Javascript files, module files can contain a class definition, a set of related classes, a library of functions, or some code to execute. The goal of modularity is to support large-scale program development, to handle the assembly of code in a decentralized source, and to allow the code to run correctly, even if it contains module code that the author does not expect to appear, and can execute the code correctly. To do this, different modules must avoid modifying the global execution context, so subsequent modules should be executed in the context of the original (or near-original) that they expect. This actually means that the module should define the global identity as little as possible, and ideally, none of the modules should define more than one (global identity).

  5. Objects used as namespaces: one way to avoid polluting global variables during module creation is to use an object as a namespace. Instead of defining global functions and variables, it stores functions and values as spatial object properties, which can be referenced by global variables.

  6. As a private namespace function: The module is defined within a function to be implemented. Variables and functions defined in a function are local members of the function and are not visible outside the function. In fact, you can use this function scope as a private namespace for a module (sometimes called a "module function")

  7. an anonymous function that executes immediately: if you want the code to run in a private namespace, simply prefix the code with "(function () {" and suffix "} ())". The opening parenthesis ensures that this is a function expression, not a function definition statement, so you can add a function name to the prefix to make the code clearer. Once the module code wind is loaded into a function, you need some way to export its public APIs tocall them outside of the module functions.

  8. A comprehensive case:

varset= (function invocation () {

function Set () {

this.values={};

this.n=0;

This.add.apply (this,arguments);

}

defining instance Methods for Set.prototype

the detailed code is omitted here

Set.prototype.contains=function (value) {

Returnthis.values.hasOwnProperty (V2s (value));

};

Set.prototype.size=function () {return n;};

Here are some of the auxiliary functions and variables used in the above method

Function V2s (val) {/*...*/}

var nextid=1;

The common API for this module is the Set() constructor

we need to get this function out of the private namespace.

so that it can be used externally, in which case we export it by returning this constructor

it becomes the value of the expression that the first line of code refers to

return Set;

} ());// execute immediately after defining the function

in this code we use the name "invocation" to emphasize that the function should be executed immediately after the definition. The name "namespace" can also be used to emphasize that this function is used as a namespace

The module function returns a constructor that assigns a value to a global variable. Returning a value already clearly indicates that the API has been exported outside of the function scope. If the module API includes multiple cells, it can return a Namespace object. For the sets module, the code can be written like this:

define a global variable to hold a collection-related module

Varcollections;

if (!collections) collections={};

defining The sets module

Collections.sets= (Functionnamespace () {

define a variety of "set " classes here, using local variables and functions

... A lot of code is omitted here ...

exporting an API by returning a Namespace object

return {

Export attribute name: local variable name

Abstractset:abstractset,

Notset:notset

};

}());

that is, the final return of an object to implement the export API multiple module units

Another similar technique is to use the module functions as constructors, which are called by new and exported by assigning them to this .

var collections;

if (!collections) collections={};

Collections.sets= (new function namespace () {

A lot of code is omitted here

Export the API to this object

This. Abstractset=abstractset;

This. Notset=notset;

//......

Note that there is no return value here

});

As an alternative, if a global namespace object has been defined, the module can directly set the properties of that object without returning anything:

var collections;

if (!collections) collections={};

collections.sets={};

(function namespace () {

A lot of code is omitted here

Export the public API to the Namespace object created above

Collections.sets.abstractset=abstractset;

collections.sets.notset=notset;//...

The export operation has been performed, and no return statement is required here

}());


This article is from "Tiger Brother's Blog" blog, please be sure to keep this source http://7613577.blog.51cto.com/7603577/1567986

JavaScript classes and Modules (ii)

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.