Z
Zoran Perak
Hi,
I have a problem defining member as instance of local object (function) when
defining object using JSON format.
Here is an example:
=== code ===
var Abc = function(a, b, c) {
this.a = a;
this.b = b;
this.c = c;
}
var obj = {
Xyz : function(a, y, z) {
this.x = x;
this.y = y;
this.z = z;
},
CONST : 'some value',
member1 : new Abc(1, 2, 3), // <-- OK
member2 : new this.Xyz(1, 2, 3), // <-- error: this.Xyz is not constructor
(this==window, not obj)
prop1 : CONST // <-- error: CONST is not defined (how to reference
obj.CONST here?)
member3 : function() {
var x = new this.Xyz(1, 2, 3); // <-- OK
}
}
=== /code ===
I can define local member (member1) as new global object (Abc) but I would
like to define it as new local object (obj.Xyz).
I can instance local object inside code block (see member3) but I would like
to create local object as member (see member2).
Can someone tell me how to do this or in other words how to reference parent
object (obj) while defining members ('this' in 'member2' definition
references to global 'window' object)?
Thanks
Zoran Perak
I have a problem defining member as instance of local object (function) when
defining object using JSON format.
Here is an example:
=== code ===
var Abc = function(a, b, c) {
this.a = a;
this.b = b;
this.c = c;
}
var obj = {
Xyz : function(a, y, z) {
this.x = x;
this.y = y;
this.z = z;
},
CONST : 'some value',
member1 : new Abc(1, 2, 3), // <-- OK
member2 : new this.Xyz(1, 2, 3), // <-- error: this.Xyz is not constructor
(this==window, not obj)
prop1 : CONST // <-- error: CONST is not defined (how to reference
obj.CONST here?)
member3 : function() {
var x = new this.Xyz(1, 2, 3); // <-- OK
}
}
=== /code ===
I can define local member (member1) as new global object (Abc) but I would
like to define it as new local object (obj.Xyz).
I can instance local object inside code block (see member3) but I would like
to create local object as member (see member2).
Can someone tell me how to do this or in other words how to reference parent
object (obj) while defining members ('this' in 'member2' definition
references to global 'window' object)?
Thanks
Zoran Perak