有没有办法在JavasScript中自定义/覆盖赋值操作? [英] Is there a way to customize/override assignment operations in JavasScript?

查看:126
本文介绍了有没有办法在JavasScript中自定义/覆盖赋值操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


每次我分配字符串时,我实际上都想分配一个字符串对象,而不需要额外的代码。

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);

基本上是高举作业。


跟进:

如果以上是不可能有一种方法来原型化字符串变量类型,而不是String对象吗?

Follow-up:
If the above is not possible is there a way to prototype the string variable type, rather than the String object?

正如 armando 所指出的, foo 将是一个字符串类型,但实质上是一个自定义数组。能够将函数原型化到该类是很好的。

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.

推荐答案


  1. 不,这不是可能

  2. 如果有可能,你真的不想这样做,至少不是全球性的。







  • 字符串变量类型不具备全部对象所产生的额外开销。

    注意:创建的字符串数组(在您的情况下, foo )将有其他属性(例如 foo.length

  • 对象出现性能损失


    • The string variable type does not have all the extra overhead that an object does.
      Note: the string array that is created (in your case, foo) would have other properties (eg foo.length)
    • Objects come at a performance hit
    • 这篇关于有没有办法在JavasScript中自定义/覆盖赋值操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆