2012-11-01 45 views
2

我希望有人能帮助我。 我想通过扩展来重新声明js函数。 例如,对网站的基本js函数:redeclare javascript函数

function foo(){ 
..something here.. 
} 

我想通过自己的函数具有相同的名称重新声明它。它如何最容易做到?

编辑1.我会尽力解释更好。

有在网站上的本机代码:

Notifier = { 
    debug: false, 
    init: function (options) { 
    curNotifier = extend({ 
     q_events: [], 
     q_shown: [], 
     q_closed: [], 
     q_max: 3, 
     q_idle_max: 5, 
     done_events: {}, 
     addQueues: curNotifier.addQueues || {}, 
     recvClbks: curNotifier.recvClbks || {}, 
     error_timeout: 1, 
     sound: new Sound('mp3/bb1'), 
     sound_im: new Sound('mp3/bb2') 
    }, options); 

    if (!this.initFrameTransport() && !this.initFlashTransport(options)) { 
     return false; 
    } 
    this.initIdleMan(); 

    if (!(curNotifier.cont = ge('notifiers_wrap'))) { 
     bodyNode.insertBefore(curNotifier.cont = ce('div', {id: 'notifiers_wrap', className: 'fixed'}), ge('page_wrap')); 
    } 
    }, 
    destroy: function() { 
    Notifier.hideAllEvents(); 
    curNotifier.idle_manager.stop(); 
    curNotifier = {}; 
    re('notifiers_wrap'); 
    re('queue_transport_wrap'); 
    }, 
    reinit: function() { 
    ajax.post('notifier.php?act=a_get_params', {}, { 
     onDone: function (options) { 
     if (options) { 
      curNotifier.error_timeout = 1; 
      this.init(options); 
     } else { 
      curNotifier.error_timeout = curNotifier.error_timeout || 1; 
      setTimeout(this.reinit.bind(this), curNotifier.error_timeout * 1000); 
      if (curNotifier.error_timeout < 256) { 
      curNotifier.error_timeout *= 2; 
      } 
     } 
     }.bind(this), 
     onFail: function() { 
     curNotifier.error_timeout = curNotifier.error_timeout || 1; 
     setTimeout(this.reinit.bind(this), curNotifier.error_timeout * 1000); 
     if (curNotifier.error_timeout < 256) { 
      curNotifier.error_timeout *= 2; 
     } 
     return true; 
     }.bind(this) 
    }); 
    } 
} 

和功能健全

function Sound(filename) { 
    var audioObjSupport = false, audioTagSupport = false, self = this, ext; 
    if (!filename) throw 'Undefined filename'; 

    try { 
    var audioObj = ce('audio'); 
    audioObjSupport = !!(audioObj.canPlayType); 

    if (('no' != audioObj.canPlayType('audio/mpeg')) && ('' != audioObj.canPlayType('audio/mpeg'))) 
     ext = '.mp3?1'; 
    else if (('no' != audioObj.canPlayType('audio/ogg; codecs="vorbis"')) && ('' != audioObj.canPlayType('audio/ogg; codecs="vorbis"'))) 
     ext = '.ogg?1'; 
    else 
     audioObjSupport = false; 
    } catch (e) {} 
    // audioObjSupport = false; 

    if (audioObjSupport) { 
    audioObj.src = filename + ext; 
    var ended = false; 
    audioObj.addEventListener('ended', function(){ended = true;}, true); 
    audioObj.load(); 
    this.playSound = function() { 
     if (ended) { 
     audioObj.load(); 
     } 
     audioObj.play(); 
     ended = false; 
    }; 
    this.pauseSound = function() { 
     audioObj.pause(); 
    }; 
    } else { 
    cur.__sound_guid = cur.__sound_guid || 0; 
    var wrap = ge('flash_sounds_wrap') || utilsNode.appendChild(ce('span', {id: 'flash_sounds_wrap'})), 
     guid = 'flash_sound_' + (cur.__sound_guid++); 

    var opts = { 
     url: '/swf/audio_lite.swf?4', 
     id: guid 
    } 
    var params = { 
     swliveconnect: 'true', 
     allowscriptaccess: 'always', 
     wmode: 'opaque' 
    } 
    if (renderFlash(wrap, opts, params, {})) { 
     var swfObj = browser.msie ? window[guid] : document[guid], 
      inited = false, 
      checkLoadInt = setInterval(function() { 
     if (swfObj && swfObj.paused) { 
      try { 
      swfObj.setVolume(1); 
      swfObj.loadAudio(filename + ext); 
      swfObj.pauseAudio(); 
      } catch (e) {debugLog(e);} 
     } 
     inited = true; 
     clearInterval(checkLoadInt); 
     }, 300); 
     self.playSound = function() { 
     if (!inited) return; 
     swfObj.playAudio(0); 
     }; 
     self.pauseSound = function() { 
     if (!inited) return; 
     swfObj.pauseAudio(); 
     }; 
    } 
    } 
} 
Sound.prototype = { 
    play: function() { 
    try {this.playSound();} catch(e){} 
    }, 
    pause: function() { 
    try {this.pauseSound();} catch(e){} 
    } 
}; 

当我尝试添加注射重新声明功能听起来这是行不通的。 如果我创造我自己的功能,例如,xSound和сall这样说:

cur.sound = new xSound('mp3/bb1'); 

它的工作。

+0

您可以添加您的重新声明代码并解释失败清单如何?我可以从中推断出,如果'cur'的类型是'Notifier',并且在调用'cur.init'之后重新声明了,你可能会遇到问题,因为'cur.sound'对象是不赞成'声音'。 – bellpeace

回答

6

你可以像这样做,例如:

foo = function(args) { 
    // method body... 
} 

JavaScript是一种编程语言,其中函数是一等公民,因此您可以像其他类型的操纵它们。

UPDATE:

确保这段代码实际上做了重新定义,而不是第一个定义。 (感谢@ jmort253)

+0

不幸的是它不工作。 –

+0

很好的答案。您可能会考虑[编辑]补充说,重新定义在*初始定义之后*是重要的,以免被覆盖。这对所有人都不是显而易见的。 – jmort253

+0

@AndreiNikolaev - 首先尝试在Chrome开发者控制台中运行它。如果有效,请参阅我以前的评论。你的定义很可能被另一个foo函数覆盖。或者这是一个范围问题。您可能需要发布更多代码,以使问题更加清晰。希望这可以帮助! – jmort253

1
function foo(){ 
    // ..something else here.. 
}