我想创建一个不需要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