1

I deleted my old post as it contained a lot of spam. And I created a jsfiddle to reproduce the issue I am having.

http://jsfiddle.net/mLxSP/

console.clear();
var args = '{ action: downline }';

showObject(args);

showObject({ action: 'downline' });


function showObject(args) {

    console.log(args);        
}

How do I pass args as an object? I'm currently building args into a string dynamically and need to pass it as an object for the function to work correctly.

1
  • You could parse args explicitly to json: showObject(JSON.parse(args)). Commented Mar 30, 2012 at 18:27

5 Answers 5

2

It is not your passing, but rather your declaration that is the problem. Define args as

var args = { action: 'downline' };
Sign up to request clarification or add additional context in comments.

1 Comment

Ahh, I'm glad someone understood what I was trying to say. I don't explain myself the best sometimes. This worked like a charm, thank you very much!
0

args.action, or args[action] will show downline

you can make also your object like: args = {}; args.action = 'downline'; etc

What's the problem?

Comments

0

I am not sure what the issue is.

Your second way is how you pass an object to a function.

This fiddle shows the log: http://jsfiddle.net/maniator/mLxSP/1/

Comments

0

If you generated it as valid JSON, you could parse the string as json and display that.

console.clear();
var args = '{ "action": "downline" }';

showObject($.parseJSON(args));

showObject({ action: 'downline' });


function showObject(args) {

    console.log(args);        
}

1 Comment

Ahh, that is interesting. I will have to play around with that some.
0

Jquery "parseJSON" will help you.

$(document).ready(function() {

    var args = $.parseJSON('{"action": "downline" }');
    showObject(args);
    showObject({
        action: 'downline'
    });

    function showObject(args) {
        console.log(args);
    }
});​

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.