Recently, I have got into a habit of calling things like RegExp, String, Number, Object, TypeError, etc without "new".
e.g:
throw (TypeError("Error"));
var regex = RegExp('^word$');
I know that this is bad for cases when "this" context is needed, since without "new", "this" wreaks havoc on your global scope unless you wrap your code in 'use strict', in which case it raises an error that you are trying to mutate 'undefined'. (I am not sure that this works in very very old browsers).
e.g:
var constructor = function() {
// 'use strict'; /* uncomment this line to avoid the behavior and be warned */
this.state = 'working as intended';
};
var foo = constructor();
console.log(foo.state); // undefined
console.log(window.state); // we just polluted our global scope.
whereas
var constructor = function() {
this.state = 'working as intended';
};
var foo = new constructor;
console.log(foo.state); // "working as intended"
console.log(window.state); // we are clean.
But in cases like the ones above, is it okay to do this, or are there problems I am setting myself up for if I get into a habit of doing this?
Thanks ahead of time.