> Now, for the past few months, I've been defining my objects like this:
>
> // where `clone' = Object.extend(Object.create(proto), props)
> var Thing = function(){ return (
> { clone: _clone
> , toString: toString
> })
>
> function clone(name){ return clone(Thing, { name: name }) }
> function toString() { return '[thing ' + this.name + ']' }
> }()
>
> var Specific = function(){ return clone(Thing,
> { clone: _clone
> , toString: toString
> })
>
> function clone(name){ return clone(Specific, { name: name }) }
> function toString() { return '[specific ' + this.name + ']' }
> }()
>
> var foo = Thing.clone('foo')
> var bar = Specific.clone('bar')
Where is _clone in the above code? You also could write the functions into the
object literals then you wouldn’t need an IIFE.
How about the following variation of your code? Would that still reflect your
intentions or go against your taste? It’s very close to your next proposed
solution.
var Clonable = {
clone: function() {
var inst = Object.create(this);
inst.init.apply(inst, arguments);
},
extend: function(props) {
return Object.extend(Object.create(this), props);
},
}
var Thing = Clonable.extend({
init: function (name) {
this.name = name;
},
toString: function () {
return '[thing ' + this.name + ']';
},
});
var Specific = Thing.extend({
// reuse Thing.init()
toString: function () {
return '[specific ' + this.name + ']';
},
});
var foo = Thing.clone('foo')
var bar = Specific.clone('bar')
> /// Using an extended version of object literals
> var Thing = {
> constructor(name){ this.name = name }
> ,toString(){ return '[thing ' + this.name + ']' }
> }
>
> var Specific = Thing <| {
> constructor(name){ this.name = name }
> ,toString(){ return '[specific ' + this.name + ']' }
> }
>
> var foo = new Thing('foo')
> var bar = new Specific('foo')
That’s approximately how Irakli’s Selfish or my Prototypes as Classes work
(both are ES5 libraries).
> The one thing class-syntax would excel at, though, is the current semantics
> for super calls in ES.next. I find them particularly confusing, considering
> the relationship between functions and objects in JS. They make more sense
> with a static declarative syntax, but that might be just me.
Super-calls are the one thing that would stay the same between all current
proposals!
--
Dr. Axel Rauschmayer
[email protected]
home: rauschma.de
twitter: twitter.com/rauschma
blog: 2ality.com
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss