0

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 the string 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.

Community
  • 1
  • 1
vol7ron
  • 40,809
  • 21
  • 119
  • 172
  • @vol7ron: based on the questions that you have been asking recently, I think that you really want to be programming in Ruby. JavaScript doesn't support the kind of metaprogramming that you seem to be after. – Adam Crossland Sep 22 '10 at 14:57
  • [@Adam Crossland:](http://www.stackoverflow.com/users/226476/adam-crossland) My questions lately have no real merit. It's been a while since I've done any intense JavaScript, so I'm just kind of getting back into it (I refuse to use `Prototype` or `JQuery`). If `Ruby` could be run as part of a client-side web application, I would look more into it, but my focus is still on ECMAScript. Server-side, I'm still a `Perl` devotee. :) – vol7ron Sep 22 '10 at 15:09
  • [@Adam Crossland:](http://stackoverflow.com/users/226476/adam-crossland) In your profile, your blog points to Google - intense :) – vol7ron Sep 22 '10 at 15:10
  • I'm not sure why you'd want to do this. Every string literal in JS has the same access to `String.prototype` functions as a String object. `"zomgwtf".toUpperCase()` works fine. – MooGoo Sep 22 '10 at 15:22
  • 1
    [This](http://www.hunlock.com/blogs/The_Complete_Javascript_Strings_Reference) goes into a lot of detail about strings, literals and objects. Also, you might find [this](http://stackoverflow.com/questions/3756549/string-object-versus-literal-modifying-the-prototype) question informative. – sje397 Sep 22 '10 at 15:24
  • [@MooGoo:](http://stackoverflow.com/users/375394/moogoo) I think the main reason I would like to know is for prototyping variables on-the-fly, see my question [here](http://stackoverflow.com/questions/3757269/how-to-set-object-property-in-prototype-function-scope-problem) – vol7ron Sep 22 '10 at 15:34
  • [@sje397:](http://stackoverflow.com/users/338803/sje397) yes, that question/answer was very helpful! +1. So would adding a closure to the property list keep it in `String object` mode? Additionally, could you use a closure method to mimic a property value? – vol7ron Sep 22 '10 at 15:41
  • I give the answer at another post: https://stackoverflow.com/a/62627719/4437467 – Andre Lopes Jun 28 '20 at 20:13
  • I give the answer at another post: https://stackoverflow.com/a/62627719/4437467 – Andre Lopes Jun 28 '20 at 20:16

2 Answers2

2
  1. No this is not possible
  2. 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 (eg foo.length)
  • Objects come at a performance hit
BotNet
  • 2,759
  • 2
  • 16
  • 17
0

It's not quite what you're looking for, but you may want to look at Overriding assignment operator in JS

Community
  • 1
  • 1
samwyse
  • 2,760
  • 1
  • 27
  • 38