Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Nov 2008
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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.

  • #2
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    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

  • #3
    New to the CF scene
    Join Date
    Nov 2008
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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'});


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •