<!doctype html> <html> <head> <meta charset="utf-8"> <title>基于MVC的JagaScript Web 富应用开发</title> <script> var Class = function () { var klass = function () { this.init.apply(this, arguments); }; klass.prototype.init = function () {}; // 定义 prototype 的别名 klass.fn = klass.prototype; // 定义类的别名 klass.fn.parent = klass; // 给类添加属性 klass.extend = function (obj) { var extended = obj.extended; for (var i in obj) { klass[i] = obj[i]; } if (extended) extended(klass); }; // 给实例添加属性 klass.include = function (obj) { var included = obj.included; for (var i in obj) { klass.fn[i] = obj[i]; } if (included) included(klass); }; return klass; }; /* // 添加静态属性,将属性复制到类中 var Person = new Class; Person.extend({ find: function (id) { alert(id); }, exists: function (id) {} }); var person = Person.find(1); */ // 添加实例属性,降属性复制到类的原型中(prototype) var Person = new Class; Person.include({ save: function () {}, destroy: function () {} }); var person = new Person; person.save(); // 支持 extended 和 included 回调。降属性传入对象后就会触发这两个回调 Person.extend({ extended: function (klass) { console.log(klass, ' was extended!'); } }); </script> </head> <body> </body> </html>