2014-02-17 36 views
2

這是默認所有的JS如何重寫現有功能的jQuery

getEditor: function(){ 
    $('#datatableEditor').remove(); 
    var editor = $('<div id="datatableEditor" class="popupEditor"/>'); 
    $('body').prepend(editor); 

    var dialog = $(editor).dialog({ 
     title: 'Edit item', 
     modal: true, 
     width: 'auto', 
     height: 'auto' 
    }); 

現在我正在寫另一個JS。我需要覆蓋我的js中的getEditor ...

+0

您想要更改/實施 –

+0

我需要更改標題。對所有人來說都是一樣的,但我需要將標題改爲動作名稱 – user2819086

回答

0

您只需獲得對函數「getEditor」的引用併爲其分配另一個函數。

getEditor:function(){ 
// new function will override the existing reference that getEditor already has. 
} 
1
someObject = { 
getEditor: function(){ 
    $('#datatableEditor').remove(); 
    var editor = $('<div id="datatableEditor" class="popupEditor"/>'); 
    $('body').prepend(editor); 

    var dialog = $(editor).dialog({ 
    title: 'Edit item', 
    modal: true, 
    width: 'auto', 
    height: 'auto' 
    }); 
} 

所以只寫

someObject.getEditor = function(){ 
    // your own logic here 
} 

你必須得到存儲getEditor函數,其中的對象,並使用參考來改變它。

4

您還沒有描述你的問題,但顯然根據您在標題中提到的內容:

覆蓋jQuery的

現有的功能,看來你想改變一個jQuery功能,他們通常被定義爲$.fn.getEditor,如果是這樣的話,您應該這樣做:

(function ($) { 
    var oldGetEditor = $.fn.getEditor; 
    $.fn.getEditor = function() { 

     //you can call the oldGetEditor here if you want 
    }; 
})(jQuery);