문제

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.

도움이 되었습니까?

해결책

Use a string object instead of a string value.

i = new String("hello world");
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top