有没有办法在 JavaScript 中自定义/覆盖赋值操作?
每次分配一个字符串时,我实际上想分配一个字符串对象,而不需要额外的代码。
这个 var foo = "bar";
变成 var foo = new String("bar");
基本上劫持了作业。
后续:
如果上述不可能,是否有一种方法可以对string
变量类型而不是 String 对象进行原型设计?
正如 armando 所指出的,foo
将是一个字符串类型,但本质上是一个自定义的大批。如果能够为该类制作函数原型那就太好了。
Every time I assign a string, I'd actually like to assign a string object, without the extra code.
This var foo = "bar";
becomes var foo = new String("bar");
Basically hi-jacking the assignment.
Follow-up:
If the above is not possible is there a way to prototype thestring
variable type, rather than the String object?
As pointed out by armando, the foo
would be a string type, but is essentially a customized array. It would be nice to be able to prototype functions to that class.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
注意:创建的字符串数组(在您的例子中,
foo
)将具有其他属性(例如foo.length
)Note: the string array that is created (in your case,
foo
) would have other properties (egfoo.length
)这不完全是您正在寻找的内容,但您可能想看看 Overriding赋值运算符 in JS
It's not quite what you're looking for, but you may want to look at Overriding assignment operator in JS