...

View Full Version : Setting object variables through JSON.



edislasm
11-15-2008, 12:21 AM
Hi.

I've started coding in a OOP manner. I need a way, to set all variables specified in a JSON object to a normal object. Right now I'm trying to use the call method, but I don't know what should I put where it says "WHATHERE?". For example:

-----------------------------------------

function myObject() {
this.myvarA = null;
this.myvarB = null;
}

function JSONproperties(arguments) {
for (property in arguments[0]) {
this.WHATHERE?= arguments[0][property]; //this should set the variable named property to the value in arguments[0][property]
}
}

var obj = new myObject;
JSONproperties.call(obj, {myvarA:'hello',myvarB:'hello2'});

-------------------------------------------

I've thought of using something like the setAttribute method like:
this.setAttribute(property,arguments[0][property])
but setAttribute is a thing for Elements, not for Objects.

What can I do?. Any help will be appreciated.

rangana
11-15-2008, 07:58 AM
function JSONproperties(arguments) {
for (property in arguments) {
alert(arguments[property]); // Outpus hello and hello2
}
}

edislasm
11-15-2008, 04:49 PM
Sorry, I had forgotten... The thing then would be:

function myObject() {
this.myvarA = null;
this.myvarB = null;
}

function JSONproperties(arguments) {
for (property in arguments[0]) {
this[property] = arguments[0][property];
}
}

var obj = new myObject;
JSONproperties.call(obj, {myvarA:'hello',myvarB:'hello2'});



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum