I’ve been working on a pet project where I’ve been creating a functions only JS framework… and wasn’t sure if there was any way to simplify the following… in this case Dynamic variable assignment…
String.prototype.is = function(x) {window[this]=window[this]||x;}
"a".is(42);
alert(a); // window.alert shows 42
Is there any simpler way to do this with functions? This is for the sole purpose of achieving a functions-only framework… so using “a = 42;” is not permitted… I want this to be usable for not just numbers, but strings, arrays, booleans, dates, etc.
There’s a problem in your idea, it’s not a good practice to change the prototype of native types. This can result in an unexpected (and sometimes almost impossible to debug) behavior when using with third-party code.
No problem in your main idea, but I suggest you to wrap the objects you want to manipulate with another under your full control and so decorate these objects with the additional behavior provided by your wrapper. This is the jQuery approach, a lot safer.