JavaScript Namespace
With the prevailing usage of AJAX calls, multiple subapps (and their corresponding JS snippets) will be loaded in the same logical "page", thus sharing the same runtime namespace.
Without proper naming control, there's a high chance of different subapps having functions and variable naming conflicts, when they are "weaved" fo
Therefore, it is imperative that we have proper namespace management for our javascript functions, both for common js library, and for subapp js
All subapps are implicitly importing and inheriting the following "objects"
Javascript my itself does not have the concept of namespace.
However, we can achieve the effect of non-conflicting namespace by following a coding pattern.
For us, the following pattern is proposed.
All Javascript fields and function definitions must be defined this way (assuming subapp name is TestSubapp):
var TestSubapp = new function() {
this.var1 = "";
this.function1 = function() {
return 12345;
};
}
Instead of placing your variables and functions in the global namespace, a virtual namespace "TestSubapp" is simulated.
The virtual namespace will take the naming convention of CamelCase, using the name of the subapp.
Variables and functions declared this way can be accessed using:
TestSubapp.var1 = TestSubapp.function();
The following namespace are provided at framework level, and are automatically available to subapps
分享到:
相关推荐
命名空间,一看就知道主要目的是为了避免冲突。下面就来说一下如何打造自己的JavaScript命名空间。
用JavaScript语言创建跨浏览器的命名空间。 这包括具有更强大方法的新数据类型,以及新类,例如符合W3C的XML DOM API和WebControls。 可以随时创建新的类和名称空间。
浅析JavaScript中命名空间namespace模式_.docx
JavaScript不像C#或Java,有专门的namespace和package语法支持,当JS复杂到一定程度,尤其是引用大量的第三方JS框架和类库之后,命名冲突就会成为一个严重的问题,因此使用JS自己的变通方式建立命名空间很重要。...
在一些 JavaScript 的模块模式中,通常建议将这些模块组织到某种自定义命名空间中以减少全局混乱: // Create a namespace called 'Animal'var Animal = window . Animal || { } ;Animal . Cat = { speak : ...
This book also walks experienced JavaScript developers through modern module formats, how to namespace code effectively, and other essential topics. Learn the structure of design patterns and how ...
代码如下://调用例子:bradio.namespace(“bradio.lang.array”); //下面是百度编译后的源码,变量都是a,b,c,d…之后我会重新写一个规范点的。 //好了,开始分析 bradio.namespace = function () { //创建命名...
JavaScript 中的命名空间这是一个小函数,它为您在 JavaScript 中声明和使用命名空间提供了一个很好的、简单的语法。 除了处理命名空间之外,它不会做任何事情:例如,它不会尝试加载其他脚本或模块。 它遵循我的...
但是我前些日子也改过些许客户端的小bug,确实那代码看得让人有些云里雾里,不知身在哪山,轻易不敢动代码,于是就自己动手鼓捣起我曾又爱又恨的javascript来,自己写一个简单的js实现namespace,继承,重载等面向...
JavaScript创建命名空间(namespace)通过自定义函数进行类型判断、数组遍历、函数执行等相关操作来实现命名空间的功能,需要的朋友可以参考一下
一、开源项目 Javascript .NET ... (缺点就是asp.net web应用上,好象会报错) 示例代码: 代码如下:using Noesis.Javascript; using System;... namespace JsCSharp { class Program { static void Main(string
js-namespace-rails可以通过使用它的方法来命名空间并根据哪个Rails控制器动作处于活动状态来选择性地执行某些JavaScript,从而解决此问题。 安装 将此行添加到您的应用程序的Gemfile中: gem 'js-namespace-rails...
运行命令:./bin/jade-namespace test.jade 使用命名空间内的已编译 javascript 函数生成 javascript。 例如,如果我们有一个名为 tests/test.jade 的 jade 文件,其中包含以下内容: h1 Hi 它变成了具有以下...
Basil.js 是一个 JavaScript 的数据持久化层库,实现了统一的 LocalStorage、Cookie 和 Session 存储。基本使用:basil = new window.Basil(options); // basic methods basil.set('foo', 'bar'); // store...
github:https://github.com/hirokidaichi/namespace-js 定义Namespace对象: var Namespace 现在来具体看一下Namespace对象的定义,它是一个NamespaceDefinition对象。该对象是一个函数对象(NamespaceDefinition...
安装$ npm install postcss-namespace例子 // dependenciesvar fs = require ( "fs" )var postcss = require ( "postcss" )var namespace = require ( "postcss-namespace" )// css to be processedvar css = fs ....
$ npm install postcss-selector-namespace 用法 var postcss = require ( 'postcss' ) var selectorNamespace = require ( 'postcss-selector-namespace' ) var output = postcss ( ) . use ( selectorNamespace ...