JavaScriptinheritance框架

是否有一个小的,轻量级的JavaScript类inheritance的解决scheme,可以在客户端和服务器端(node.js)很好地工作? 我不想要一个大型的图书馆,只是让我声明一个构造函数和一些方法,然后有一个类inheritance的能力。

John Resig在这里介绍了大约25行代码中的一个简单的inheritance框架。 我已经看到它曾经效果很好。

你可以像这样使用它:

var Vehicle = Class.extend({ init: function(wheels) { this.wheels = wheels; } }); var Truck = Vehicle.extend({ init: function(hp, wheels) { this.horsepower = hp; this._super(wheels); }, printInfo: function() { console.log('I am a truck and I have ' + this.horsepower + ' hp.'); } }); var t = new Truck(4, 350); t.printInfo(); 

我创build了这个小型库来使用ExtJs风格ClassManager。 这很简单,但非常灵活。

通过node.js安装

 npm install esf-core 

样品

 Esf.define('A', { a: null, constructor: function (a) { // Save var this.a = a; // Heyho console.log('A'); }, foo: function (b) { console.log('foo - ' + b); } }); Esf.define('B', { b: null, constructor: function (a, b) { // Call super constructor this.callParent(a); // Save var this.b = b; // Heyho console.log('B'); }, foo: function () { this.callParent('bar'); } }, { extend: 'A' }); // Use var b = new B(1, 2); // or var b = Esf.create('B', 1, 2); /* * Output: * A * B * foo - bar */ b.foo(); 

知识库

https://bitbucket.org/tehrengruber/esf-js-core

我已经看到了原型库的成功使用。

我认为这比简单inheritancefw中的init hax要好得多:

 (function() { var core = { require : function(source) { if ( typeof (source) != "object" || !source) throw new TypeError("Object needed as source."); for (var property in source) if (source.hasOwnProperty(property) && !this.prototype.hasOwnProperty(property)) this.prototype[property] = source[property]; }, override : function(source) { if ( typeof (source) != "object" || !source) throw new TypeError("Object needed as source."); for (var property in source) if (source.hasOwnProperty(property)) this.prototype[property] = source[property]; }, extend : function(source) { var superClass = this; var newClass = source.hasOwnProperty("constructor") ? source.constructor : function() { superClass.apply(this, arguments); }; newClass.superClass = superClass; var superClone = function() { }; superClone.prototype = superClass.prototype; newClass.prototype = new superClone(); newClass.prototype.constructor = newClass; if (source) newClass.override(source); return newClass; } }; core.require.call(Function, core); Function.create = function (source){ var newClass = source.hasOwnProperty("constructor") ? source.constructor : function() {}; newClass.override(source); return newClass; }; })(); 

这个车辆的例子是:

 var Vehicle = Function.create({ constructor : function(wheels) { this.wheels = wheels; } }); var Truck = Vehicle.extend({ constructor : function(hp, wheels) { this.horsepower = hp; Vehicle.call(this, wheels); }, printInfo : function() { console.log('I am a truck and I have ' + this.horsepower + ' hp.'); } }); var t = new Truck(4, 350); t.printInfo(); 

我创build了一个非常轻量级的库,可以在浏览器和node.js中使用。 它是一个非常易于使用的,庞大的图书馆:

例:

 var Person = proto(function() { // prototype builder this.init = function(legs, arms) { // constructor this.legs = legs this.arms = arms } this.getCaughtInBearTrap = function() { // instance method this.legs -= 1 } this.limbs = function() { return this.arms + this.legs } }) var Girl = proto(Person, function() { // inheritance this.haveBaby = function() { return Person(2,2) } }) var g = Girl(2,2) // instantiation g.getCaughtInBearTrap() console.log("Girl has "+g.limbs()+" limbs") console.log(": (")