I need to make a change but continue providing backwards-compatibility for some time, but I can’t figure out how to accomplish this.
I have an Object like this:
MyObject = (function() {
// vars here and there
var myVar
// a bunch of code
return {
get myVar() { return myVar },
myVar: function() { return myVar }
}
}())
This doesn’t work. I want to be able to get myVar both with
MyObject.myVar
and
MyObject.myVar()
Is it possible? Even better, is there any possibility to accomplish that and make it cross-browser compatible? (Right now I know the get notation is not supported by Internet Explorer)
Thanks.
UPDATE:
Because it seems not possible, is there any way to make what I have now, get myVar() browser-compatible with Internet Explorer?
So all browsers can access MyObject.myVar
As long as I can continue providing a getter to myVar and it’s compatible with Internet Explorer, there is no need to move to myVar() but I couldn’t find a feasible solution to make the current way (MyObject.myVar) compatible other than moving to a function-like getter.
It IS possible, though your code will look rather nasty. Since functions are objects, they have a
valueOfproperty, too. Exploit that by overriding it with a reference to the method itself, and you’re there:Be advised, its type will be an object all the same, so strict comparisons won’t work:
This can cause lots of nasty bugs that could leave you scratching your hair for up to the point there’s no hair left to scratch. Not to mention other people that might end up maintaining your code!