I am trying to create a specific syntax template in JavaScript. Essentially, the requirement is to extend prototypes of native JavaScript objects with new methods and organize these methods in namespaces. The result I'm looking for can be summarized as:
String.prototype.foo = {
bar: function() { return this.toUpperCase(); }
};
'test'.foo.bar();
I know that there are other ways to achieve similar results, but I would prefer the syntax to be higher, although I am almost 80% sure that this is not so.
I tried a dozen different ways to build this, but I can't find a way to reach the string like "this" inside bar (). I believe the problem is that since foo is an object and does not have executable code, the compiler cannot pass the owner of βthisβ to it from there to the child method, but I wondered if there was some obscure technique for walking around Does the parent chain exist that I don't know about? The next best thing might be "test..foo (). Bar (), which I have not tried yet, but will most likely work.
source
share