我想創建一個不需要DOM元素但有默認選項的插件。如何用選項創建jQuery插件但沒有元素?
當我運行下面的代碼時,我得到TypeError: $.myApp is not a function
。
我需要改變什麼?
;(function ($, window, document, undefined) {
"use strict";
var defaults = {
val: 'old value'
};
function myApp (options) {
this.settings = $.extend({}, defaults, options);
this._defaults = defaults;
this.init();
};
$.extend(myApp .prototype, {
init: function() {
console.log (this.settings);
}
});
})(jQuery, window, document);
$(function() {
$.myApp({
val: 'new value'
});
});
你可以試試這個http://jsfiddle.net/k7h90y9h/ – guvenckardas
對不起,正確的鏈接是http://jsfiddle.net/k7h90y9h/2/ – guvenckardas