Comments (14)
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.
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.
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.
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.
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.
Hm... you shouldn't have to do that -- would you mind explaining again what's happening to you?
from klass.
i think he just likes it stylistically and has nothing to do with klass.
from klass.
With the new code, initialize will only be executed 1 time invocation ( you have to call supr directly).
from klass.
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.
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.
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.
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.
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.
great -- glad everything is working for you :D
from klass.
Related Issues (20)
- initialize() being applied to all klass() Objects after an .extend() HOT 2
- Klass.js doesn't work HOT 12
- Semi-colon at the end HOT 5
- Bug: self.supr property can get out of sync HOT 3
- extend doesn't include statics HOT 2
- Unable to hardcode super's constructor argument values from a subclass constructor HOT 3
- how to access "statics" ? HOT 1
- Extremly slow compared to .inheritWith, .fastClass, ExtJS, TypeScript or Native solutions HOT 4
- no closing semicolon in the minified version HOT 1
- Please provide release tag for used version in PhotoSwipe HOT 1
- Missing License HOT 1
- How to use klass with event.Emitter HOT 2
- Suggest a .constants() method like .statics() which defines values you cannot change. HOT 1
- Mixin calling its own thing instead of super's thing
- Test case not testing anything (constructor bubbling)
- Add Klass to bower. HOT 2
- Tagged versions. HOT 2
- Klass doesn't work in strict mode. HOT 1
- Leaking global 'supr' symbol when calling supr using methods without proper this context
- Codemod to port to es2015 classes?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from klass.