Giter Club home page Giter Club logo

Comments (14)

ded avatar ded commented on August 15, 2024

I think @fat broke something.
In the meantime, this will work as advertised.

var Object1 = klass(function() {
  console.log('Init 1');
});

var Object2 = Object1.extend(function() {
  console.log('Init 2');
});

var Object3 = Object2.extend(function() {
  console.log('Init 3');
});

var myObject = new Object3();

outputs:

Init 1
Init 2
Init 3

from klass.

ded avatar ded commented on August 15, 2024

another aside: this works too, which you might like:

var Object1 = klass({
    init: function() {
        console.log('Init 3');
        return this;
    }
});

var Object2 = Object1.extend({
    init: function() {
        console.log('Init 2');
        this.supr();
        return this;
    }
});

var Object3 = Object2.extend({
    init: function() {
        console.log('Init 1');
        this.supr();
        return this;
    }
});

var myObject = new Object3().init();

from klass.

fat avatar fat commented on August 15, 2024

This is fixed with the latest klass -- we implemented your "point 1" ... which is, when initialize is used, the constructor won't bubble.

thnaks for bringing this up!

from klass.

stephenmelrose avatar stephenmelrose commented on August 15, 2024

Cheers boys.

To get around my point 2 we'll probably use,

var BaseKlass = new klass({
    initialize: function() {
        this.init();
    },
    init: function() {}
});

And use BaseKlass as our base object and init() as our constructor.

Thanks again.

from klass.

ded avatar ded commented on August 15, 2024

just remember that the auto-propagation (a feature i really like) is always there for you when you need it. simply pass a function as an argument to klass and all is magic.

from klass.

fat avatar fat commented on August 15, 2024

Hm... you shouldn't have to do that -- would you mind explaining again what's happening to you?

from klass.

ded avatar ded commented on August 15, 2024

i think he just likes it stylistically and has nothing to do with klass.

from klass.

fat avatar fat commented on August 15, 2024

With the new code, initialize will only be executed 1 time invocation ( you have to call supr directly).

from klass.

fat avatar fat commented on August 15, 2024

his new code won't work. well... it will.. but it's wrong to do that... initliaze does what he wants by default now.

from klass.

ded avatar ded commented on August 15, 2024

his new code will work. i think you're reading it wrong. he just wants to separate his init into a separate method. initalize will be called, then it calls init.

from klass.

fat avatar fat commented on August 15, 2024

in his initial example:

var Object1 = klass({
    initialize: function() {
        console.log('Init 1');
    }
});

var Object2 = Object1.extend({
    initialize: function() {
        console.log('Init 2');
    }
});

var Object3 = Object2.extend({
    initialize: function() {
        console.log('Init 3');
    }
});

The following is now outputted,

init 3

from klass.

stephenmelrose avatar stephenmelrose commented on August 15, 2024

Balls. Sorry, I replied to this on my iPhone in the pub, got my point 1 and point 2 mixed up.

This works exactly as I would expect it to now. I thought you'd only fixed the weird executing the third initialize() three times bug, and not stopped it auto-executing too.

So to achieve,

Init 1
Init 2
Init 3

I can now write,

var Object1 = klass({
    initialize: function() {
        console.log('Init 1');
    }
});

var Object2 = Object1.extend({
    initialize: function() {
        this.supr();
        console.log('Init 2');
    }
});

var Object3 = Object2.extend({
    initialize: function() {
        this.supr();
        console.log('Init 3');
    }
});

var myObject = new Object3();

Which is exactly what I'd expect.

The code I posted was incase initialize() still automatically called the supr() for you. In my code the constructor would only exist on a base object and call my init() once, which I can then extend and call supr() within myself, essentially moving the constructor to another method giving me full control. But you sorted that so my point and code is completely a moo point.

So, cheers again boys and sorry for the confusion!

from klass.

stephenmelrose avatar stephenmelrose commented on August 15, 2024

And as you pointed out @ded, you can still make it automatically call the supr() by using the non-"little curlies" method too. So now it's purely a stylistic choice. Lurvely!

from klass.

fat avatar fat commented on August 15, 2024

great -- glad everything is working for you :D

from klass.

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.