2016-01-24 98 views
3

我有一個方法addObject jQuery原型,我已經完成使用jQuery.fn.extend()擴展一個jQuery原型

jQuery.fn.extend({ 
    addObject : function(objectKey){ 
     this[objectKey] = {}; 
    } 
}); 

現在我想添加一個方法addNewObject到它的原型。我試圖仿效下面的情形

function addObject(){ 
    ... 
} 

addObject.prototype.addNewObject = function(){ 
    ... 
} 

如何是我ADDOBJECT與new關鍵字訪問?

+2

*「我對jQuery的原型方法ADDOBJECT我已經做了使用jQuery.fn.extend()。「* *顯示*我們,不*告訴*我們。 –

+2

'jQuery.fn.addObject.prototype.addNewObject = function(){...' – adeneo

+0

'this [objectKey] = {};''的預期結果是什麼? – guest271314

回答

2

而不是使用extend,你可以使用:

// insulate plugin in IIFE 
(function() { 
    // declare function 
    function addObject(options) { 
     return this.each(function(){}); 
    } 
    // add prototypes 
    addObject.prototype.addNewObject = function() {} 

    // extend as plugin by passing function reference 
    $.fn.addObject = addObject; 


})(jQuery); 

https://jqueryboilerplate.com/對大量的信息,指南,模式等,爲開發插件