Go Back   CodingForums.com > :: Client side development > JavaScript programming > DOM and JSON scripting

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-15-2008, 12:21 AM   PM User | #1
edislasm
New to the CF scene

 
Join Date: Nov 2008
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
edislasm is an unknown quantity at this point
Exclamation Setting object variables through JSON.

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.
edislasm is offline   Reply With Quote
Old 11-15-2008, 07:58 AM   PM User | #2
rangana
Senior Coder

 
rangana's Avatar
 
Join Date: Feb 2008
Location: Cebu City, Philippines
Posts: 1,752
Thanks: 65
Thanked 372 Times in 365 Posts
rangana will become famous soon enoughrangana will become famous soon enough
Code:
function JSONproperties(arguments) {
for (property in arguments) {
alert(arguments[property]); // Outpus hello and hello2
}
}
__________________
Learn how to javascript at 02geek

The more you learn, the more you'll realize there's much more to learn
Ray.ph
rangana is offline   Reply With Quote
Old 11-15-2008, 04:49 PM   PM User | #3
edislasm
New to the CF scene

 
Join Date: Nov 2008
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
edislasm is an unknown quantity at this point
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'});
edislasm is offline   Reply With Quote
Reply

Bookmarks

Tags
dom, json, oop

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:46 AM.


Advertisement
Log in to turn off these ads.