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

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

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