Thought of js package, namespace or module again
来源:互联网 发布:老子西出函谷关 知乎 编辑:程序博客网 时间:2024/06/07 06:48
How to define a js package?
In orginal PIES, you should first declare the package in some where:
$package.register('a.b.c', './lib/example.js#X,Y;Z');
Then u can import it in any program:
$import(a.b.c);
...
var x = new X();
After importing, X and Y will be export into global scope.
If you want to use Z, you should write:
var p = $import(a.b.c);
...
var z = new p.Z();
This syntax and semantic is to align mozilla's js 2.0 draft.
But the new es4 spec is based on adobe's draft.
And I have read a lot of projects which provide some js package mechanism, and rethought the thing.
Below is my new design:
1. Define a new module:
new Module().
$import('a.b.X').
include('./test1.js').
include('./test2.js').
$export('a.b.Y').
end;
2. Import package:
$import(a.b.Y);
...
var x = new Y();
There is a significent change, instead of register a package, I choose to define a module. What's the difference? That is, the new design separate the package(namespace), module and source files.
A package is just a namespace, which is the name system to organize the code logically (and identify the creator).
A module is about scope, and the dependency. You can import many names(package) in a module or define many names(package) in a module.
In the traditional js practice, module and file are one to one map. But, currently there are many js compressor or packer or linker...In many big sites, a many small js source files will be pack into a single compressed js file, so just like jar for java or assembly for .net. That means module and deployment files should be separated.
My new design seperate these three things.
new Module().
$import('a.b.X').
include('./test1.js').
include('./test2.js').
$export('a.b.Y').
end;
These codes defines an new moudle, which includes to small source files test1.js and test2.js, and in these two files, they can reference X object, which under package a.b (so it has the full quarlified name a.b.X), and in one of the files, it will define a object Y, which will be exported into package a.b (so with the full quarlified name a.b.Y). This module will depend on another module which exports a.b.X, and any modules which import a.b.Y will depend on it (but you not need to define the dependency explictly).
I am also developing jsa(js achive or js assembly), which can pack many modules into one file. Not like simply merge, it won't cause name conflict.
- Thought of js package, namespace or module again
- node.js 模块(Module)和包(Package)
- Module & Package
- Node.js 的包(package)和模块(module)
- 5. node.js 的 模块(Module)和包(Package)
- R运行错误:Error: package or namespace load failed for ‘rJava’:
- JavaScript Namespace and Package
- Package and Namespace
- namespace 与 package
- [翻译]On the role of scientific thought
- malformed or corrupted AST file。。。module file out of date'
- malformed or corrupted AST file。。。module file out of date'
- malformed or corrupted AST file。。。module file out of date'
- Exception about "Could not load file or assembly Namespace.Components' or one of its dependencies."
- JS NameSpace
- perl package和module
- python module package
- import module package mechisiam
- 点对点传输
- 二叉树的基本运算实验
- 文章三篇
- Hooking KiFastSystemCall
- OOP在三层系统中的应用
- Thought of js package, namespace or module again
- Jbpm Delegation机制源代码分析和实例
- JSIntegration
- C.M. Coolidge画的狗
- 核心(Core) Javascript 学习手记
- Friendly URLs in Tapestry
- 工作日志2006.11.26
- Tomcat Mysql 。。。学习笔记
- 产生高质量成比例缩略图