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(initiali zer, o) {
if (!o) o = Object;
function f() {}
f.prototype = o;
obj = new f();
obj.__progenito r = o;
if (initializer) {
if ((typeof(initia lizer)).toLower Case() != "function") throw
"Expected initializer to be of type function, got type " +
typeof(initiali zer);
initializer.app ly(obj);
}
return obj;
}
Now assume these uses of the object() function:
SimplePattern = object();
Prototype = object(
function() {
this.prototypeV alue = "Yo";
this.message = function(messag e) {alert(message) ;}
},
Object
);
Thing = object(
function() {
var secret = 42;
var _outer = this;
_outer.GiveUpSe cret = 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.GiveUpSe cret = function() { return secret; }
_outer.subObjec t = object(
function initializer() {
var _inner = this;
_inner.subObjec tProp = 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?
--
Jeff S.