Instancing local objects using JSON notation

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top