Code Monkey home page Code Monkey logo

Comments (7)

maccman avatar maccman commented on June 3, 2024

I soniciq, I did that with Super.js before - unfortunately it had it's issues - you could never be sure what was available when creating modules etc. It's the same argument for having a core library with Ruby, for example, or always including models with Rails apps.

What we could start doing though, is use sprockets. Although there would be a bundled file containing everything, you could always use the individual source files if you just wanted say, class support. What do you think?

from spine.

 avatar commented on June 3, 2024

Sprockets sounds like the way forward... I am currently using a simplified version of your Class object, you will see that I am using 'klass' and 'superclass' properties in place of your 'parent' property as I commonly find myself needing to use 'parent' when dealing with tree structures etc. Am I on the right lines with those names? My head hurts when it comes to prototypal inheritance but thanks to the simplicity of your code I think I've finally grasped it...

var Class = {
  prototype: { initializer: function() {} },

  subclass: function(include, extend){
    var object = Object.create(this);
    object.superclass = this;
    object.prototype = Object.create(this.prototype);

    if (include) { object.include(include); }
    if (extend) { object.extend(extend); }

    return object;
  },

  init: function(){
    var initance = Object.create(this.prototype);
    initance.klass = this;
    initance.initializer.apply(initance, arguments);
    return initance;
  },

  include: function(obj){
    for (var key in obj) { this.prototype[key] = obj[key]; }
    return this;
  },

  extend: function(obj){
    for (var key in obj) { this[key] = obj[key]; }
    return this;
  }
};

from spine.

 avatar commented on June 3, 2024

Sorry for the double comment but is there any way to adapt the Class object to allow for instantiating with the 'new' keyword? I prefer the prototypal inheritance but have a lot of code that instantiates with 'new' using John Resig's Simple Inheritance. I basically want to replace my class implementation but not have to rewrite legacy code.

from spine.

maccman avatar maccman commented on June 3, 2024

Yes, I was not sure about the naming scheme either, specially as a instances
parent is the class, and a classes parent is the class it inherits from -
probably a bit confusing. The real parent is proto. Anyways...

As to your question, unfortunately it's not possible. You can only use the
'new' keyword with constructor functions, and unfortunately functions don't
allow you to proper inheritance - static properties have to be copied. It's
for this reason that Prototype's class implementation does't inherit static
(i.e. class) properties.

On Thu, Apr 28, 2011 at 3:38 PM, soniciq <
[email protected]>wrote:

Sorry for the double comment but is there any way to adapt the Class object
to allow for instantiating with the 'new' keyword? I prefer the prototypal
inheritance but have a lot of code that instantiates with 'new' using John
Resig's Simple Inheritance. I basically want to replace my class
implementation but not have to rewrite legacy code.

Reply to this email directly or view it on GitHub:
https://github.com/maccman/spine/issues/11#comment_1073433

Alex MacCaw

(415) 513-0975
@maccman

http://alexmaccaw.co.uk | http://www.leadthinking.com | http://socialmod.com

from spine.

 avatar commented on June 3, 2024

So I suppose 'parent/klass/superclass' aren't needed as proto always returns the parent?

Thanks for the clarification on the 'new' keyword, that makes sense... I've been writing javascript for 10 years and never really understood prototypal inheritance properly. It seems 'new' just adds complexity that's not really needed.

from spine.

 avatar commented on June 3, 2024

I've just noticed that 'instanceof' doesn't work with this approach... am I missing something?

from spine.

 avatar commented on June 3, 2024

You may find this useful... I cant' get instanceof to work as it only works with instances created with a constructor but if you add the following to the Class.prototype, you get a kindOf method that works like Ruby's is_a/kind_of:

prototype: { 
  initialize: function() {},

  kindOf: function(klass) {
    var match = this.klass;          
    while (match && match !== klass) { match = match.superclass; }
    return match === klass;
  }
}

from spine.

Related Issues (20)

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.