GithubHelp home page GithubHelp logo

nicolas-van / ring.js Goto Github PK

View Code? Open in Web Editor NEW
221.0 12.0 9.0 750 KB

Ring.js - JavaScript Class System with Multiple Inheritance

Home Page: https://nicolas-van.github.io/ring.js/

License: MIT License

JavaScript 96.93% HTML 3.07%
javascript object-oriented class

ring.js's Introduction

Ring.js

Build Status npm version

Ring.js is a class system in JavaScript allowing multiple inheritance.

var Human = ring.create({
  talk: function() {
    return "hello";
  },
});

var Spider = ring.create({
  climb: function() {
    return "climbing";
  },
});

var SpiderMan = ring.create([Spider, Human], {
  talk: function() {
    return this.$super() + ", my name is Peter Parker";
  }
});

var spiderman = new SpiderMan();
console.log(spiderman.talk());

Its advantages:

  • Stop fighting against JavaScript prototype-based object oriented system. Use a class system like you would in Java, Python, or basically any well-known language.
  • Available in the browser or in node.js. Also works with require.js.
  • The inheritance system is inspired by Python, one of the best multiple inheritance system ever.
  • Provides compatibility with other JavaScript class systems, like CoffeeScript or Backbone.
  • Licensed under the MIT license.
  • Damn, multiple inheritance just rocks!

To get started, read the tutorial.

There is also a FAQ available here.

ring.js's People

Contributors

dependabot[bot] avatar krstffr avatar nicolas-van avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ring.js's Issues

Without underscore?

Hello there

This is a very nice bower component but I am somehow unsure whether I should use it or not. This because I am avoiding underscore. Underscore is heavy and I try to keep my app lightweight.

I wonder if it's possible to come without underscore or at least to extract only the necessary underscore functions to keep code slim?

Invoke Base Method

I like the concept of Ring.js from an OOP point of view. However, invoking a base method from a sub-type is not possible from what I gather.

To call the overriding base method, it's this.$super(args);

But what about within another method this.$super.other(args);?

For example:

var Child = ring.create([Parent],{
    baz: function(){
        this.$super.bar(); //Method not available
                this.$super().bar(); //Method not available.  Equivalent to Parent.bar().bar()
    },
    bar: function(){//New Implementation}
});

var Parent = rung.create({
    foo: function(){},
    bar: function(){}       
});

I can achieve what I need through misdirection, though I don't much like this method.

var Child = ring.create([Parent],{
    baz: function(){
        this._bar(); 
    },
    bar: function(){//New Implementation}       
});

var Parent = rung.create({
    _foo:function(){},
    _bar:function(){},
    foo: function(){this._foo()},
    bar: function(){this._bar()}        
});

Static attributes

Hi,

to my knowledge it is not possible to define static class properties (attributes and methods) in the ringJs constructor.
Is there is really no way to do that? Would be really nice to have ;)

Ring along with others

Great project - it has a lot of features I was after.

Do you think there's a chance on looking into documenting (or providing) interoperability with other libraries / languages, like Bakcbone.Model or coffeescript's class ?

Regards

FAQ: How can this.$super possibly work w/o significant performance penalty?

How is this.$super implemented in such a way that it doesn't incur a performance penalty? I mean, this.$super is a reference to a different function in the body of each method, right? How is this possible without wrapping each overriding method in a function that sets this.$super and calls the actual function?

Weird Constructor Name

In section Constructor at documentation page, you described

When the dictionary of properties given to ring.create() contains a method named constructor, that method will be called during the object construction with the argument

But it's not working at all

var A = ring.create({
    constructor: function(name) {
        this.name = name;
    },
});
var a = new A("Nicolas");
console.log(a.name); //  <- print "undefined"

However if I change constructor to init

var A = ring.create({
    init: function(name) { // change constructor to init
        this.name = name;
    },
});
var a = new A("Nicolas");
console.log(a.name); // print "Nicolas" <- it works well

How can it happend? Does document wrong?

Explanation of spec failure (bad equality)

I have the following example:

var A = ring.create({

});

var C = {};

var B = ring.create({
    item: A,
    item2: C
});

var b = new B();

expect(b.item).toEqual(A);
expect(b.item2).toEqual(C);

Test 1 fails, test 2 succeeds.
The second needs to succeed due to this example closely resembling Backbone Model / Collection relationships.

It appears to be morphing the function somehow:

Before

function Instance() {
    this.$super = null;
    this.__ringConstructor__.apply(this, arguments);
}

After

function r(){return(this instanceof r?e:n).apply(t,arguments)}

How to invoke superclass initialisation?

I guess the title says it all

I declare a subclass C of both classes A and B, both of which have an init function.
How do I invoke A.init and B.init from C.init?

Nicole

Multiple inheritance method resolution

Hi,

ring.js looks great for us. But I found an issue I can't resolve, and which is not covered by docs. If I do multiple inheritance in object C from objects A and B, both implementing method .testMe() and both calling this.$super() in this method I have both A.testMe() and B.testMe() called but in the end I get an error exception:

var
  A = ring.create({
    testMe: function() { console.log('A'); this.$super() }
  }),           

  B = ring.create({
    testMe: function() { console.log('B'); this.$super() }
  }),

  C = ring.create([A, B], {
    testMe: function() { console.log('C'); this.$super() }
  });

  (new C).testMe();

https://www.dropbox.com/s/aqa5ga577djm3g6/%D0%A1%D0%BA%D1%80%D0%B8%D0%BD%D1%88%D0%BE%D1%82%202014-11-30%2019.06.02.png?dl=0

In other way — if I don't call this.$super() in A.testMe() and B.testMe() I have just one method called in C.testMe() and no exception:

var
  A = ring.create({
    testMe: function() { console.log('A') }
  }),           

  B = ring.create({
    testMe: function() { console.log('B') }
  }),

  C = ring.create([A, B], {
    testMe: function() { console.log('C'); this.$super() }
  });

  (new C).testMe();

https://www.dropbox.com/s/2qp3qyd6nuurjcz/%D0%A1%D0%BA%D1%80%D0%B8%D0%BD%D1%88%D0%BE%D1%82%202014-11-30%2019.07.52.png?dl=0

What am I doing wrong? Maybe some concepts are misunderstood by me?

Static / Class variable inheritance support

Take the following example:

var A = ring.create({
});

A.staticVar = 'foo';

var B = ring.create([A], {
});

console.log(A.staticVar); // 'foo'
console.log(B.staticVar); // undefined

Problem with length

var X = ring.create({});

var Y = ring.create([X], {length: 33});

new Y();

I saw strange stuff in the prototype of Y.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.