首页 > 代码库 > AMD and CMD are dead之KMD规范

AMD and CMD are dead之KMD规范

What’s KMD?

乱世出英雄,KMD名字的由来充满了杀气。

Kill AMD and CMD

KMD为替代混乱的AMD和CMD世界而生,一统天下。或者让这个混乱的世界更加混乱,导致:

KMD AMD CMD三分天下

KMD的目标从来都是远大的:

JS工程化终极解决方案

使用KMDjs的工程师从来都是:

image

尼玛,什么东西,这么NB?

KMD规范

1.通过define定义命名空间和类

define("MyApp.User", {    init: function (name,age) {        this.name = name;        this.age = age;    },    say: function () {        alert("my name is " + this.name);    }})

其中MyApp是命名空间,User是类

2.不允许只定义类,不定义命名空间,如:下面的做法是错误的:

define("User", {    init: function (name,age) {        this.name = name;        this.age = age;    },    say: function () {        alert("my name is " + this.name);    }})
<style type="text/css">.csharpcode, .csharpcode pre{ font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em;}.csharpcode .lnum { color: #606060; }</style>

3.通过statics给类定义静态属性和方法:

define("NS2.Model.Animate",["NS2"], {    statics: {        Version: "1.0",        CreateGUID: function () {            return 2;        }    },    init: function (name, age) {        this.name = name;        this.age = age;    }})

4.通过冒号实现继承:

define("NS2.Model.User:NS2.Model.Animate", {    statics: {        CreateGUID: function () {            return 3;        }    },    init: function (name, age) {        this.name = name;        this.age = age;    }})
<style type="text/css">.csharpcode, .csharpcode pre{ font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em;}.csharpcode .lnum { color: #606060; }</style>

4.静态属性可继承,可覆盖,父类的保持不变

5.通过require方法引用命名空间,引用之后,该命名空间下的类全部可以使用

require(["NS", "NS2", "NS2.Model"], function () {    //这里面可以使用上面三个命名空间下的所有类})
<style type="text/css">.csharpcode, .csharpcode pre{ font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em;}.csharpcode .lnum { color: #606060; }</style>

这里不需要担心会加载大量Class,KMDjs会拿到function(){}去分析用来哪个命名空间下的哪个类,然后按需加载执行。

6.通过_super访问父类方法:

define("NS2.Model.User:NS2.Model.Animate", {    init: function (name, age) {        this._super(name, age);        this.sex = "男";    }})

7.require第一个参数支持string,该参数直接代表命名空间,引用多个命名空间,使用数组。

require("NS", function () {    //这里可以使用NS下的所有Class})

8.js文件名和类名一致

9.类名和命名空间的关系单独建立文件

KMDjs网址

http://kmdjs.org/ 备案中..

http://kmdjs.github.io/  kmdjs在github官网

http://htmlcssjs.duapp.com/kmdjs/  kmdjs在BAE

kmdjs@QQ.com kmdjs官方邮箱

@当耐特 kmdjs官方微博

总结

AMD and CMD are dead. That‘s all.

我不是认真,我就是为了输赢.