Javascript:javascript对象属性字符串中的特殊字符。

时间:2018-10-10 15:25:04

标签: javascript string object

我有一个带有某些对象/属性的变量,我需要在其中之一上添加商标。这可能吗?

var something = {
    name: "This is trademarked",
    //some more objects
};

我尝试以这种方式应用它:

var trademark = document.write("\u2122");
var something = {
    name: "This is trademarked" + trademark,
    //some more objects
};

但是显示为未定义。

2 个答案:

答案 0 :(得分:4)

var something = {
  name: "This is trademarked",
  //some more objects
};

var trademark = "\u2122";

something.name += trademark;

// this is just to show the result, don't used it in a real case
document.write(something.name)

答案 1 :(得分:0)

Eduardo的解决方案使我进入了对我有用的解决方案,以防将来有人看到它。

var trademark = "\u2122";
var something { 
    name: "some text " + trademark,
    //more object
}