javascript - requirejs dependency function's this is window -


i use requirejs , 1 dependency is

define(function(){      .....    fusionchart = function(){        this.xxxx = ....;//the statement catch wrong,this window not object   }    return fusionchart; }); 

i'm new requirejs,i don't know why,and how avoid it

there nothing wrong require here. "this" in inner functions points window.

read http://javascriptweblog.wordpress.com/2010/08/30/understanding-javascripts-this/

if fusionchart meant constructor function then:

define(function(){     var fusionchart = function(){        this.x = 0;     };     return fusionchart; }); 

elsewhere in code:

define(function(){     var fusionchart = require('fusionchart');     var fusionchartinstance = new fusionchart(); }); 

you can make fusionchart behave singleton returning new fusionchart(); won't have create instance when require it. requirejs point same reference.

define(function(){     var fusionchart = function(){        this.x = 0;     };     return new fusionchart(); });  define(function(){     var fusionchartinstance = require('fusionchart'); }); 

Comments

Popular posts from this blog

C# random value from dictionary and tuple -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -

cgi - How do I interpret URLs without extension as files rather than missing directories in nginx? -