【发布时间】:2016-05-27 11:51:59
【问题描述】:
忽略所有最佳实践,我有一个相当学术的问题,出于好奇,我会问。
如您所知,您可以为字符串对象添加自定义属性:
var s = new String("initial string");
var s.customProperty = "Please Don't Forget about me";
是否可以(不创建新的字符串对象)更改 "s" 引用的字符串原语?
具体来说,我想更改 "s"(上图),以便 s == "new string",但在进行此更改后,我想要 s.customProperty 仍然指向原始字符串:“请不要忘记我”。同样,我想在不必创建任何新字符串对象的情况下实现这一点。
var s = new String("initial string");
var s.customProperty = "Please Don't Forget about me";
// What code must preceed the following "if statement" in order
// for it to return "true" (giving the restrictions I described
// in the paragraphs of this post)?
if(s == "new string" && s.customProperty == "Please Don't Forget about me")
{
console.log("true");
}
else
{
console.log("false");
}
换句话说,是否可以在不丢失或克隆已添加到该字符串对象的所有自定义属性的情况下更改字符串对象的引用指针?
【问题讨论】:
标签: javascript string ecmascript-6 ecmascript-5