J
Jeff Stewart
I've been working with the JavaScript Shell in Firefox on a
mad-scientist problem I've had in my head. Assume a function named
'object' that is designed to create an object based on a prototype
object -- the semantic is that the returned object prototypally
inherits from the argument 'o'.
function object(initializer, o) {
if (!o) o = Object;
function f() {}
f.prototype = o;
obj = new f();
obj.__progenitor = o;
if (initializer) {
if ((typeof(initializer)).toLowerCase() != "function") throw
"Expected initializer to be of type function, got type " +
typeof(initializer);
initializer.apply(obj);
}
return obj;
}
Now assume these uses of the object() function:
SimplePattern = object();
Prototype = object(
function() {
this.prototypeValue = "Yo";
this.message = function(message) {alert(message);}
},
Object
);
Thing = object(
function() {
var secret = 42;
var _outer = this;
_outer.GiveUpSecret = function() { return secret; }
},
Prototype
);
These all produce the results I would expect. Typical prototypal
inheritance.
But if I alter the Thing definition's initializer to include a member
that should inherit from Prototype,
Thing = object(
function() {
var secret = 42;
var _outer = this;
_outer.GiveUpSecret = function() { return secret; }
_outer.subObject = object(
function initializer() {
var _inner = this;
_inner.subObjectProp = 73;
},
Prototype
);
},
Prototype
);
Thing all of a sudden is -missing- its GiveUpSecret() function, has no
subObject member, and reports its prototypeValue as "Yo".
Why doesn't this work more intuitively?
mad-scientist problem I've had in my head. Assume a function named
'object' that is designed to create an object based on a prototype
object -- the semantic is that the returned object prototypally
inherits from the argument 'o'.
function object(initializer, o) {
if (!o) o = Object;
function f() {}
f.prototype = o;
obj = new f();
obj.__progenitor = o;
if (initializer) {
if ((typeof(initializer)).toLowerCase() != "function") throw
"Expected initializer to be of type function, got type " +
typeof(initializer);
initializer.apply(obj);
}
return obj;
}
Now assume these uses of the object() function:
SimplePattern = object();
Prototype = object(
function() {
this.prototypeValue = "Yo";
this.message = function(message) {alert(message);}
},
Object
);
Thing = object(
function() {
var secret = 42;
var _outer = this;
_outer.GiveUpSecret = function() { return secret; }
},
Prototype
);
These all produce the results I would expect. Typical prototypal
inheritance.
But if I alter the Thing definition's initializer to include a member
that should inherit from Prototype,
Thing = object(
function() {
var secret = 42;
var _outer = this;
_outer.GiveUpSecret = function() { return secret; }
_outer.subObject = object(
function initializer() {
var _inner = this;
_inner.subObjectProp = 73;
},
Prototype
);
},
Prototype
);
Thing all of a sudden is -missing- its GiveUpSecret() function, has no
subObject member, and reports its prototypeValue as "Yo".
Why doesn't this work more intuitively?