I'm trying to achieve a specific syntax pattern in JavaScript. Essentially, the requirement is to extend the prototypes of native JavaScript objects with new methods, and to organise those methods into namespaces. The result I'm looking for can be summed up as:
String.prototype.foo = {
bar: function() { return this.toUpperCase(); } //Fails because "this" is foo, not it's parent string
};
'test'.foo.bar(); //should return 'TEST'
I am aware there are other ways to achieve similar results but I really would prefer the syntax above is possible - although I'm about 80% sure that it's not.
I've tried a dozen different ways of constructing this but I can't find any way of reaching the string as "this" inside bar(). I believe the problem is that because foo is an object and has no executable code, there is no way for the compiler to pass the owner "this" to it and from there down to the child method, but I wondered if some obscure technique for walking the parent chain exists that I don't know about? The next best thing might be 'test'.foo().bar(), which I haven't tried, but sounds more likely to work.