Is there a way to customize/override assignment operations in JavasScript?
Every time I assign a string, I'd actually like to assign a string object, without the extra code.
This var foo = "bar";
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 protot开发者_如何转开发ype functions to that class.
- No this is not possible
- If it was possible, you really would not want to do this, at least not globally.
- 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 (egfoo.length
) - Objects come at a performance hit
It's not quite what you're looking for, but you may want to look at Overriding assignment operator in JS
精彩评论