2

The function below "foo" doesn't work. I would like a way to get the output shown below, working around the principle shown in "foo".

function foo() {
    var i;

    i = "hello world";
    i.a = "hello kitten";
    i.b = "hello... Is there anybody out there?"

    return i; // This doesn't work
}

This is what I want:

    alert(foo()); // "hello world"
    var bar = foo();
    alert(bar.a); // "hello kitten"
    alert(bar.b); // "hello... Is there anybody out there?"

Thanks.

1 Answer 1

4

Use a string object instead of a string value.

i = new String("hello world");
Sign up to request clarification or add additional context in comments.

2 Comments

In that case, alert(foo()); might show something like { [String: 'hello world'] a: 'hello kitten', b: 'hello... Is there anybody out there?' }?
Thank you sir! Brilliant (and so quick!). Works perfectly, implementing now. (Will wait a few hours to be polite before I accept).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.