0

一直在尝试创建一个始终包含公共字符串的 JavaScript 对象成员。每当我创建一个新对象时,它不是连接字符串,而是用创建时传递的值覆盖它。如果重要(我认为不重要),则字符串包含数字。标准杆示例:

function myObj(strToConcat){
    this.combinedString = "Hello " + strToConcat, /* have used + and .concat() without success */
}

var newObj = new myObj("1.2.3");
console.log(newObj.combinedString); /* says "1.2.3", the leading "Hello " is absent */

似乎无法将其连接起来。

编辑:我很抱歉,错误超出了我认为负责的代码。请无视。我很抱歉。

4

2 回答 2

2

You have error in your reference

console.log(myObj.combinedString);

should be

console.log(newObj.combinedString);
于 2013-03-28T15:50:42.623 回答
1

运行你的代码给了我SyntaxError: Unexpected token }. ,将第二行末尾的 a 替换为 a ,;我得到“Hello 1.2.3”的预期结果。

于 2013-03-28T15:54:01.297 回答