2012-11-01 5 views
2

누군가가 나를 도울 수 있기를 바랍니다. 확장 기능으로 js 기능을 다시 선언하고 싶습니다.자바 스크립트 기능 다시 선언

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

내가 같은 이름을 가진 자신의 함수를 재 선언 할 : 예를 들어, 웹 사이트의 기본 JS 기능이있다. 어떻게하는 것이 가장 쉬운가?

편집 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

재 선언 코드를 추가하고 실패가 어떻게 나타 납니까? Curs가'Notifier' 타입이고'cur.init'이 호출 된 후에 다시 선언이 왔을 때,'cur.sound' 객체가 'Sound'는 더 이상 사용되지 않습니다. – bellpeace

답변

6

당신은 예를 들어, 이런 식으로 작업을 수행 할 수 있습니다

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

자바 스크립트는 다른 유형처럼 조작 할 수 있도록 기능이 일류 시민 프로그래밍 언어입니다.

UPDATE :

코드의이 작품은 실제로 재정이 아닌 첫 번째 정의를하지 있는지 확인합니다. (덕분에 @ jmort253)

+0

불행히도 작동하지 않습니다. –

+0

좋은 답변입니다. 덮어 쓰기되지 않도록 다시 정의하는 것이 중요하다는 것을 [편집]이라고 생각할 수도 있습니다. 모두에게 분명하지 않을 것입니다. – jmort253

+0

@AndreiNikolaev - Chrome 개발자 콘솔에서 먼저 실행 해 봅니다. 작동하는 경우 내 이전 댓글을 참조하십시오. 정의가 다른 foo 함수로 덮어 쓰게 될 가능성이 있습니다. 또는 범위 문제입니다. 문제가 좀 더 명확 해 지도록 더 많은 코드를 게시해야 할 수도 있습니다. 희망이 도움이! – jmort253

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