Every time I assign a string, I'd actually like to assign a string object, without the extra code.
这个var foo = "bar";
变为 var foo = new String("bar");
基本上是劫持作业。
<小时/>Follow-up:
If the above is not possible is there a way to prototype thestring
variable type, rather than the String object?
正如 armando 所指出的,foo
将是一个字符串类型,但本质上是一个自定义数组。如果能够为该类制作函数原型(prototype)那就太好了。
最佳答案
- 不,这是不可能的
- 如果可能的话,您真的不会想这样做,至少在全局范围内不会这样做。
- 字符串变量类型没有对象所具有的所有额外开销。
注意:创建的字符串数组(在您的例子中为foo
)将具有其他属性(例如foo.length
) - 对象会影响性能
关于javascript - 有没有办法在 Javascript 中自定义/覆盖赋值运算符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3770480/