On 30/10/11 09:45, Rick Waldron wrote:
On Oct 30, 2011, at 5:58 AM, David Bruant<bruan...@gmail.com> wrote:
Le 30/10/2011 02:35, Quildreen Motta a écrit :
(...)
Are we overthinking classes?
Perhaps the reason for all this thinking about classes come from the
role constructor functions take in the language? I'm a bit sceptical
on constructors and constructor's own properties being that important,
though.
I agree. Has anyone used constructor properties for "static" properties?
Just considering the DOM, constants are put on the prototype and
everyone sounds happy with it so far.
In jQuery, John/we put "static" methods and properties on the jQuery() function -
this practice is used for anything that is not a selector match operation. This includes
Ajax and Deferred (among others). Considering jQuery is used on>26.6million websites,
it's safe to bet that practice is in common use.
Rick
Ah, when I said `important' I didn't mean in the sense of how much
people use it. I meant that the functionality can be achieved with or
without constructors.
The only thing that changes is the order of importance of things. With
constructors, the major player are functions — which arguably makes the
semantics of prototypes a bit weird:
function Thing(parent, name) {
// instance properties
this.name = name
this.childs = []
if (parent) parent.childs.push(this)
}
// constructor properties
Thing.sort = function(thing){
thing.childs.sort(function(o1, o2){ return o1.name < o2.name })
return thing
}
// shared instance properties
Thing.prototype.sort = function(){
return Thing.sort(this)
}
var something = new Thing(null, 'something')
something.sort()
Thing.sort(something)
With objects as the major players, you still get "static" properties,
due to the nature of delegative behaviour sharing that [[Prototype]]
has, it's just a little different:
Thing = {
// instance properties
new: function(parent, name) {
this.name = name
this.childs = []
if (parent) parent.childs.push(this)
},
// shared instance properties
sort: function() {
this.childs.sort(function(o1, o2){ return o1.name < o2.name })
return this
},
// "constructor" properties
static: {
sort: function(thing) {
return Thing.sort.call(thing)
}
}
}
var something = Thing.new(null, 'something')
something.sort()
something.static.sort(something)
Of course, people seem to be too used to the class-like way of doing
things, that proposing such could be regarded as "madness". Plus,
getting rid of constructors is not reasonable at this point in time; as
Brendan said, we're basically stuck with every semantic quirk the
language has got so far.
_______________________________________________
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss