English 中文(简体)
我的简单事件处理实例为什么不可行?
原标题:Why does my simple event handling example not work?

我正试图做一个简单的事手。

(说明,我不想试图实施完全模糊的出版/登记模式;我只想为什么我的例子不像我认为应该那样工作)

var myObj = (function () {

  var private = "X";

  function triggerEvent(eventName) {
    if (this[eventName]) {
      this[eventName]();
    }
  }

  // Setter / Getter
  function getProp() {
    return private;
  }

  function setProp(value) {
    private = value;
    triggerEvent("onPropChange");
  }

  // Public API
  return {
    // Events
    "onPropChange": null,    // Fires when prop value is changed

    // Methods
    "getProp": getProp,
    "setProp": setProp
  };
})();

// Now set event handler
myObj.onPropChange = function () {
  alert("You changed the property!");
};

myObj.setProp("Z");  // --> Nothing happens. Wrong
                     // Why doesn t my alert show?

我把我的物体的<代码>onPropChange财产设定为更简单的手稿功能,但并未发射。 我已对此做了改动,似乎在<条码>上。 。 我认为,它应当参考<代码>myObj(这是我所需要的)。

谁能解释我的想法中的错误,我如何纠正? 帮助非常受赞赏。

j 此处为

最佳回答
问题回答

暂无回答




相关问题
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.

热门标签