admin管理员组

文章数量:1287811

Is there a small, lightweight solution for javascript class inheritance that will work well on both client and server side (node.js)? I'm not wanting a big library, just something that will allow me to declare a constructor and some methods, then have the ability for a class to inherit that.

Is there a small, lightweight solution for javascript class inheritance that will work well on both client and server side (node.js)? I'm not wanting a big library, just something that will allow me to declare a constructor and some methods, then have the ability for a class to inherit that.

Share Improve this question asked Feb 23, 2012 at 6:12 LordZardeckLordZardeck 8,29319 gold badges65 silver badges119 bronze badges 2
  • Maybe you like CoffeeScript's class and extends keywords? – Thilo Commented Feb 23, 2012 at 6:14
  • I absolutely hate coffescript. I like my code to have punctuation, and be readable. Coffescript is too much like those horrible languages such as Visual Basic and scripting languages like ruby. – LordZardeck Commented Feb 23, 2012 at 6:27
Add a ment  | 

6 Answers 6

Reset to default 6

John Resig outlines a simple inheritance framework in about 25 lines of code here. I have seen it used to good effect.

You can use it like this:

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();

take a look at https://github./ded/klass

I created this small library to use an ExtJs Style ClassManager. It's quite simple yet, but very flexible.

Install via node.js

npm install esf-core

Sample

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();

Repository

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

I've seen the prototype library used successfully.

I think this is much better than the init hax in the simple inheritance fw:

(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;
    };
})(); 

The vehicle example with this:

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();

I created a very lightweight library that works in-browser and in node.js. Its a super easy-to-use, bloatless library:

  • https://github./fresheneesz/proto

Example:

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(": (")

本文标签: javascript inheritance frameworkStack Overflow