3

高度なコンパイルモードでエラーが発生します。

Uncaught TypeError: Object #<d> has no method 'attachEvent'

ソースマップの魔法の後で、これは呼び出しからスローされることがわかりましたgoog.events.listen。最初の引数は、を継承するカスタムオブジェクトgoog.events.EventTargetです。

これはクロージャのソースにあります

goog.events.EventTarget.prototype.addEventListener = function(
    type, handler, opt_capture, opt_handlerScope) {
  goog.events.listen(this, type, handler, opt_capture, opt_handlerScope);
};

したがって、この関数は、私のオブジェクトのプロトタイプになりますcustomEvent_ = truegoog.events.listen

// Attach the proxy through the browser's API
if (src.addEventListener) {
  if (src == goog.global || !src.customEvent_) {
    src.addEventListener(type, proxy, capture);
  }
} else {
  // The else above used to be else if (src.attachEvent) and then there was
  // another else statement that threw an exception warning the developer
  // they made a mistake. This resulted in an extra object allocation in IE6
  // due to a wrapper object that had to be implemented around the element
  // and so was removed.
  src.attachEvent(goog.events.getOnString_(type), proxy);
}

(最後の行はスローする行です)

これはスタックオーバーフローで終わるべきではありませんか?オブジェクトがからelse継承する場合、なぜブランチに入るのですか?単純なコンパイルモードでは、すべてが正常に機能します。これはどのように機能しますか?また、高度なコンパイルモードでのみエラーが発生するのはなぜですか?addEventListenerEventTarget

4

1 に答える 1

1

コードを追加できますか?カスタムイベントはどのように見えますか? 基本的に、JS のデフォルトの「addEventListener」を書き直しています。IE は addEventListener を実装していませんが、 attachEvent と返されるイベント オブジェクトのみが通常のイベントではなく、goog.events.BrowserEventです。

Advanced Compilation モードでは、コンパイラはイベント オブジェクトを含むすべてのオブジェクトのプロパティをフラット化 (最小化) します。カスタム イベントのプロパティは、高度なコンパイル モードで平坦化される可能性があり (これはおそらくここでのケースです)、このため、プロトタイプに attachEvent() は存在しません。aE() などになっている可能性があります。誰かが真に有用な提案を思いつく前に、さらにコードを追加してください。

于 2012-10-21T15:27:00.837 に答える