Is it possible to extend native prototypes from within a module?
e.g.
// foo.js
String.prototype.foo = function(){ return "foo'd!"; };
// myapp.js
require('foo');
"bar".foo() // No fail
If so, is it likely to still be possible next week and 6 months from
now?
If not, how are you supposed to extend native prototypes in CommonJS?
~Daniel Friesen (Dantman, Nadir-Seen-Fire) [http://daniel.friesen.name]
Thomas Aylott / subtleGradient wrote:
> I'm new to this list and I was unable to find anything in the list
> archive or wiki that answered my question. If this has been handled
> already I'll be happy with a RTSW (read the stinkin' wiki) + link.
> Please forgive my pathetic google-fu. etc�
> </ml-n00b-boilerplate>
>
> Is it possible to extend native prototypes from within a module?
> e.g.
>
> // foo.js
> String.prototype.foo = function(){ return "foo'd!"; };
>
> // myapp.js
> require('foo');
> "bar".foo() // No fail
>
>
> If so, is it likely to still be possible next week and 6 months from
> now?
> If not, how are you supposed to extend native prototypes in CommonJS?
>
> --
>
> You received this message because you are subscribed to the Google Groups "CommonJS" group.
> To post to this group, send email to comm...@googlegroups.com.
> To unsubscribe from this group, send email to commonjs+u...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/commonjs?hl=en.
>
>
>
// footools.js
function FooTools(){
this.String.prototype.foo = function(){ return "foo'd"; };
};
exports.setup = function(context){
FooTools.apply(context);
}
if (typeof this != 'undefined') exports.setup(this);
// app.js
require('footools').setup(this);
"bar".foo(); // no Fail
// web.html
<script src="mod.js"></script>
<script>"bar".foo(); /*no Fail*/</script>
You're not supposed to. Globals in a module are not guaranteed to be
global, they could be frozen or only be a module specific copy.
Wes proposed a pattern:
require('some-prototyping-module').prototypeString(String);
"bar".foo();
((Actually it wasn't "prototypeString", I can't remember the method name
pattern he proposed.))
~Daniel Friesen (Dantman, Nadir-Seen-Fire) [http://daniel.friesen.name]
Thomas Aylott / subtleGradient wrote:
> I'm new to this list and I was unable to find anything in the list
> archive or wiki that answered my question. If this has been handled
> already I'll be happy with a RTSW (read the stinkin' wiki) + link.
> Please forgive my pathetic google-fu. etc…
A) We never defined what `this` would be inside of a module, that's an
implementation detail, and heck it's perfectly valid behavior for a
CommonJS implementation to make `this === module`.
B) In ES5 strict mode `this` will always be undefined.
C) If it does manage to work, you end up with unexpected behavior, and
running that method twice for code that is written properly.
Side note, but he got it wrong anyways. You already noted to me those
arguments should be the standard classes, NOT a global. The global
object does not need to be accessible to the user as a variable.
> Wes
>
> --
> Wesley W. Garland
> Director, Product Development
> PageMail, Inc.
> +1 613 542 2787 x 102
if (typeof this != 'undefined') exports.setup(this);
How is this version then?
if (typeof exports == 'undefined') var exports = {};