Question

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.

Était-ce utile?

La solution

Use a string object instead of a string value.

i = new String("hello world");
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top