I feel a bit dump but there is something I don’t understand (or even know if it’s possible) about prototyping in JavaScript.
I’d like to use a method while I’m creating the prototype of my pseudo-class :
var Class = function() {}
Class.prototype = {
a: function() {
return 'ok'
}
, z: Class.prototype.a() // I tried with `this`/`constructor`/etc.
} // TypeError: Object [object Object] has no method 'a' the rest isn't evaluated
var test = new Class()
test.z
I know I can do it this way but I’d like to know if I can still but all my method/properties in the Class.prototype declaration :
var Class = function() {}
Class.prototype.a = function() {
return 'ok'
}
Class.prototype.z = Class.prototype.a()
var test = new Class()
test.z // "ok"
Thanks.
No, you can’t. Just like you can’t refer to any object properties before the statement that defines them has ended:
The variable
xdoes not have a value (it is declared, since declarations are hoisted, but it’s value isundefined) until the assignment expression in its entirety has been evaluated.