《JS学习笔记》word版.doc_第1页
《JS学习笔记》word版.doc_第2页
《JS学习笔记》word版.doc_第3页
《JS学习笔记》word版.doc_第4页
《JS学习笔记》word版.doc_第5页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

JS学习笔记一:Creating Modules and Namespaces。/the first rule of JavaScript modules: a module should never add more than a single symbol to the global namespace./ Create an empty object as our namespace/ This single global symbol will hold all of our other symbolsvar Class = ;/ Define functions within the namespaceClass.define = function(data) /* code goes here */ Cvides = function(o, c) /* code goes here */ In the case of the class module, you can put the code in a file named Class.js and begin that file with a comment that looks like this:/* * Class.js: A module of utility functions for working with classes. * * This module defines a single global symbol named Class. * Class refers to a namespace object, and all utility functions * are stored as properties of this namespace. */Example 10-1. Creating a namespace based on a domain name/ Create the global symbol com if it doesnt exist/ Throw an error if it does exist but is not an objectvar com;if (!com) com = ;else if (typeof com != object) throw new Error(com already exists and is not an object);/ Repeat the creation and type-checking code for the next levelif (!com.davidflanagan) com.davidflanagan = else if (typeof com.davidflanagan != object) throw new Error(com.davidflanagan already exists and is not an object);/ Throw an error if com.davidflanagan.Class already existsif (com.davidflanagan.Class) throw new Error(com.davidflanagan.Class already exists);/ Otherwise, create and populate the namespace with one big object literalcom.davidflanagan.Class = define: function(data) /* code here */ , provides: function(o, c) /* code here */ ;var com; / Declare global symbol before testing for its presenceif (!com | !com.davidflanagan | !com.davidflanagan.Class) throw new Error(com/davidflanagan/Class.js has not been loaded);/* * com/davidflanagan/Shapes.js: a module of classes representing shapes * * This module defines classes within the com.davidflanagan.shapes namespace * This module requires the com/davidflanagan/Class.js module */ First, check for the Class modulevar com; / Declare global symbol before testing for its presenceif (!com | !com.davidflanagan | !com.davidflanagan.Class) throw new Error(com/davidflanagan/Class.js has not been loaded);/ Import a symbol from that modulevar define = com.davidflanagan.Class.define;/ We know from the test for the Class module that the com.davidflanagan/ namespace exists, so we dont have to create it here./ We just create our shapes namespace within it.if (com.davidflanagan.shapes) throw new Error(com.davidflanagan.shapes namespace already exists);/ Create the namespacecom.davidflanagan.shapes = ;/ Now define classes, storing their constructor functions in our namespacecom.davidflanagan.shapes.Circle = define( /* class data here */ );com.davidflanagan.shapes.Rectangle = define( /* class data here */ );com.davidflanagan.shapes.Triangle = define( /* class data here */);(function( ) / Define an anonymous function. No name means no global symbol / Code goes here / Any variables are safely nested within the function, / so no global symbols are created.)( ); / End the function definition and invoke it./ This is an easier name, to save typing.var define = com.davidflanagan.Class.define; ?important things to understand about importing symbols1you can import only symbols that refer to a function, object, or array。2Module developers must always use the fully qualified name of their symbols./ Create a simple namespace. No error checking required. The/ module user knows that this symbol does not exist yet.var Class = ;/ Now import a symbol into this new namespace.Class.define = com.davidflanagan.Class.define;/ A property of primitive type; cannot be importedcom.davidflanagan.Class.counter = 0;/ Here is an accessor method that can be importedcom.davidflanagan.Class.getCounter = function( ) return com.davidflanagan.Class.counter;/ Create the namespace object. Error checking omitted here for brevity.var com;if (!com) com = ;if (!com.davidflanagan) com.davidflanagan = ;com.davidflanagan.Class = ;/ Dont stick anything into the namespace directly./ Instead we define and invoke an anonymous function to create a closure/ that serves as our private namespace. This function will export its/ public symbols from the closure into the com.davidflanagan.Class object/ Note that we use an unnamed function so we dont create any other/ global symbols.(function( ) / Begin anonymous function definition / Nested functions create symbols within the closure function define(data) counter+; /* more code here */ function provides(o, c) /* code here */ / Local variable are symbols within the closure. / This one will remain private within the closure var counter = 0; / This function can refer to the variable with a simple name / instead of having to qualify it with a namespace function getCounter( ) return counter; / Now that weve defined the properties we want in our private / closure, we can export the public ones to the public namespace / and leave the private ones hidden here. var ns = com.davidflanagan.Class; ns.define = define; vides = provides; ns.getCounter = getCounter;)( ); / End anonymous function definition and invoke it/* * Module.js: module and namspace utilities * * This is a module of module-related utility functions that are * compatible with JSAN-type modules. * This module defines the namespace Module. */ Make sure we havent already been loadedvar Module;if (Module & (typeof Module != object | Module.NAME) throw new Error(Namespace Module already exists);/ Create our namespaceModule = ;/ This is some metainformation about this namespaceModule.NAME = Module; / The name of this namespaceModule.VERSION = 0.1; / The version of this namespace/ This is the list of public symbols that we export from this namespace./ These are of interest to programers who use modules.Module.EXPORT = require, importSymbols;/ These are other symbols we are willing to export. They are ones normally/ used only by module authors and are not typically imported.Module.EXPORT_OK = createNamespace, isDefined, registerInitializationFunction, runInitializationFunctions, modules, globalNamespace;/ Now start adding symbols to the namespaceModule.globalNamespace = this; / So we can always refer to the global scopeModule.modules = Module: Module ; / Module name-namespace map./* * This function creates and returns a namespace object for the * specified name and does useful error checking to ensure that the * name does not conflict with any previously loaded module. It * throws an error if the namespace already exists or if any of the * property components of the namespace exist and are not objects. * * Sets a NAME property of the new namespace to its name. * If the version argument is specified, set the VERSION property * of the namespace. * * A mapping for the new namespace is added to the Module.modules object */Module.createNamespace = function(name, version) / Check name for validity. It must exist, and must not begin or / end with a period or contain two periods in a row. if (!name) throw new Error(Module.createNamespace( ): name required); if (name.charAt(0) = . | name.charAt(name.length-1) = . | name.indexOf(.) != -1) throw new Error(Module.createNamespace( ): illegal name: + name); / Break the name at periods and create the object hierarchy we need var parts = name.split(.); / For each namespace component, either create an object or ensure that / an object by that name already exists. var container = Module.globalNamespace; for(var i = 0; i parts.length; i+) var part = partsi; / If there is no property of container with this name, create / an empty object. if (!containerpart) containerpart = ; else if (typeof containerpart != object) / If there is already a property, make sure it is an object var n = parts.slice(0,i).join(.); throw new Error(n + already exists and is not an object); container = containerpart; / The last container traversed above is the namespace we need. var namespace = container; / It is an error to define a namespace twice. It is okay if our / namespace object already exists, but it must not already have a / NAME property defined. if (namespace.NAME) throw new Error(Module +name+ is already defined); / Initialize name and version fields of the namespace namespace.NAME = name; if (version) namespace.VERSION = version; / Register this namespace in the map of all modules Module.modulesname = namespace; / Return the namespace object to the caller return namespace;/* * Test whether the module with the specified name has been defined. * Returns true if it is defined and false otherwise. */Module.isDefined = function(name) return name in Module.modules;/* * This function throws an error if the named module is not defined * or if it is defined but its version is less than the specified version. * If the namespace exists and has a suitable version, this function simply * returns without doing anything. Use this function to cause a fatal * error if the modules that your code requires are not present. */Module.require = function(name, version) if (!(name in Module.modules) throw new Error(Module + name + is not defined); / If no version was specified, there is nothing to check if (!version) return; var n = Module.modulesname; / If the defined version is less than the required version or if / the namespace does not declare any version, throw an error. if (!n.VERSION | n.VERSION 1 & typeof arguments1 = object) if (arguments1 != null) to = arguments1; firstsymbol = 2; / Now get the list of specified symbols for(var a = firstsymbol; a arguments.length; a+) symbols.push(argumentsa); / If we were not passed any symbols to import, import a set defined / by the module, or just import all of them. if (symbols.length = 0) / If the module defines an EXPORT array, import / the symbols in that array. if (from.EXPORT) for(var i = 0; i from.EXPORT.length; i+) var s = from.EXPORTi; tos = froms; return; / Otherwise if the modules does not define an EXPORT_OK array, / just import everything in the modules namespace else if (!from.EXPORT_OK) for(s in from) tos = froms; return; / If we get here, we have an explicitly specified array of symbols / to import. If the namespace defines EXPORT and/or EXPORT_OK arrays, / ensure that each symbol is listed before importing it. / Throw an error if a requested symbol does not exist or if / it is not allowed to be exported. var allowed; if (from.EXPORT | from.EXPORT_OK) allowed = ; / Copy allowed symbols from arrays to properties of an object. / This allows us to test for an allowed symbol more efficiently. if (from.EXPORT) for(var i = 0; i from.EXPORT.length; i+) allowedfrom.EXPORTi = true; if (from.EXPORT_OK) for(var i = 0; i from.EXPORT_OK.length; i+) allowedfrom.EXPORT_OKi = true; / Import the symbols for(var i = 0; i symbols.length; i+) var s = symbolsi; / The name of the symbol to import if (!(s in from) / Make sure it exists throw new Error(Module.importSymbols( ): symbol + s + is not defined); if (allowed & !(s in allowed) / Make sure it is a public symbol throw new Error(Module.importSymbols( ): symbol + s + is not public and cannot be imported.); tos = froms; / Import it ;/ Modules use this function to register one or more initialization functionsModule.registerInitializationFunction = function(f) / Store the function in the array of initialization functions Module._initfuncs.push(f); / If we have not yet registered an onload event handler, do so now. Module._registerEventHandler( );/ A function to invoke all registered initialization functions./ In client-side JavaScript, this will automatically be called in/ when the document finished loading. In other contexts, you must/ call it explicitly.Module.runInitializationFunctions = function( ) / Run each initialization function, catching and ignoring exceptions / so that a failure by one mod

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论