English 中文(简体)
模块模式中的这一关键词?
原标题:this keyword in module pattern?

我刚刚开始在一家新公司工作,并且注意到在我的许多联合材料中完全错误的东西。 我很犹豫不决,不确认这一点是错误的,因为Im pre 草率,我不是联合材料专家,它只是我的第二天,我不想 look。

因此,我通常期望模块模式能够看一看:

MODULENAME = MODULENAME || {};

MODULENAME.SUBMODULENAME = (function() {
    var bla = {};

    bla.somefunction = function() {
        //do stuff
    };

    //add more stuff to bla
    return bla;
}());

他们在其法典中的一切内容是:

MODULENAME = MODULENAME || {};

MODULENAME.SUBMODULENAME = (function() {
    var that = this;

    that.somefunction = function() {
        //do stuff
    };

    //add more stuff to that
    return that;
}());

当然,现在该功能被称作“新this, 约束为window, 并重新界定, <>>>> >> > > > > > > 因此,它们基本上将一切放在全球物体上,其所有次位名称实际上都是<代码>window的别名。 是否有任何理由想这样做? 或者,这真像我所认为的那样是错误的?

Edit:

在分单元定义之前,我错误地将<代码>var置于方括号中,最初我写出一些稍有不同之处,并想删除<代码>var。 我试图使这一榜样变得更清楚,希望它更加明显,我现在指的是什么。

<><>><>>>>

我也审视了在火焰中执行的文字,这些文字肯定在<条码>上增加了所有内容。

最佳回答

是的,它是错误的。

MODULENAME = MODULENAME || {}; // missing var

var MODULENAME.SUBMODULENAME = (function() { // probably the missing var from above...
    var that = this;
    //add some stuff to that
    return that; // that is the WINDOW- wrong.
}());

>>DEMO/strong>,因损坏:

var x = function() {
    alert( out );
}
var MODULENAME = MODULENAME || {};

MODULENAME.SUBMODULENAME = (function() {
    var that = this;
    that.x = function() {
        alert( DAMAGE );
    }
}());

x();​ // alert DAMAGE and not "out" - messed up with the global object!
问题回答

模块模式被错误地使用,如果功能表述的使用对功能声明没有规定,就不应使用。 如果打算建立全球职能(我怀疑是),那么它们应当使用:

function somefuncion() {
  ...
}

如果其意图是将财产(此处为方法)添加到更可能属于这种情况的物体,则:

MODULENAME.SUBMODULENAME.somemethod = function() { /* do stuff */ };

如果需要有条件地制定方法,例如基于特征检测的方法,那么以下做法可能适用:

(function(global, undefined) {

  // In here global is the global object
  global.MODULENAME = global.MODULENAME || {};
  global.MODULENAME.SUBMODULENAME = global.MODULENAME.SUBMODULENAME || {};

  // and undefined is undefined, belt and braces approach
  undefined = void 0;

  // Direct assignment
  function somemethod() {
      //do stuff      
  };

  // Assign directly to the "namespace" object
  MODULENAME.SUBMODULENAME.somemethod = somemethod;

  // Conditional assignment
  if ( sometest ) {
    MODULENAME.SUBMODULENAME.anothermethod = function(){...};

  // Try another way...
  } else if (someOtherTest) {
    MODULENAME.SUBMODULENAME.anothermethod = function(){...};

  // Default
  } else {
    MODULENAME.SUBMODULENAME.anothermethod = function(){...};
  }

  // Clean up 
  global = null;

}(this)); 

上文提到的一个问题是,在外部职能中宣布的每一项职能都关闭了功能目标及其环境,从而浪费了资源。 保持简单化和只使用模块模式,使之真正需要,并在有标记的地方公正使用平原功能声明和任务,则效率更高。 不是虚弱,而是更实际的。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.