gpt4 book ai didi

javascript - jQuery 插件 + AMD = 如何访问函数?

转载 作者:行者123 更新时间:2023-11-28 15:51:37 25 4
gpt4 key购买 nike

我正在 AMD 环境中封装我的 jQuery 插件。这是我的样板,

!function(root, factory) {
if (typeof define === 'function' && define.amd) {
define(['jquery'], factory);
} else {
factory(root.jQuery);
}
}(this, function($) {

var defaults = {
target: ''
};

var myPlugin = function(options) {
options = $.extend(true, {}, defaults, options);

return options;
};

myPlugin.prototype = {
init: function(options) {
return options;
}
};

$.fn.myPlugin = myPlugin;

});

console.log($.fn.myPlugin.init());

错误,

TypeError: $.fn.myPlugin.init is not a function

console.log($.fn.myPlugin.init());

你知道我做错了什么吗?如何访问 myPlugin.prototype = {...} 中的函数?

编辑:

用此测试 boilerplate ,

console.log($('.test li').plugin({
test: 'option1',
test2: 'option2'
}));

结果,

Object[] // why is it empty?

还有

console.log($.fn.plugin.someMethod());

结果,

TypeError: $.fn.plugin.someMethod is not a function

console.log($.fn.plugin.someMethod());

并且,

// Plugin methods and shared properties
Plugin.prototype = {
// Reset constructor - http://goo.gl/EcWdiy
constructor: Plugin,

someMethod: function(options) {
return options;
}
};

console.log($.fn.plugin.Plugin.prototype.someMethod("hello world"));

结果,

hello world

并且,

var instance = $('.element').data('plugin');
instance.someMethod("hello world");

结果,

TypeError: instance is null // what does it mean? It should return 'hello world', isn't it?

instance.someMethod("hello world");

编辑2:

var plugin = $('.element').plugin();
var instance = $('.element').data('plugin',plugin);
console.log(instance); // returns - Object[]
console.log(instance.someMethod("hello world"));

结果,

TypeError: instance.someMethod is not a function

console.log(instance.someMethod("hello world"));

最佳答案

您似乎并没有真正创建 myPlugin 的实例,而是尝试静态访问这些方法,这可能是也可能不是您想要的。

我发现每次使用插件时创建一个插件对象的实例会更好。一个例子:

!function(root, factory) {
if (typeof define === 'function' && define.amd) {
define(['jquery'], factory);
} else {
factory(root.jQuery);
}
}(this, function($) {
'use strict';

var defaults = {

};

var Plugin = function(element, options) {
this.element = element;
this.options = options;
};

Plugin.prototype = {
constructor: Plugin,

someMethod: function() {

}
}

// Create the jQuery plugin
$.fn.plugin = function(options) {
options = $.extend(true, {}, defaults, options);

return this.each(function() {
var $this = $(this);
$this.data('plugin', new Plugin($this, options));
});
};

// Expose defaults and Constructor
$.fn.plugin.defaults = defaults;
$.fn.plugin.Plugin = Plugin;
});

从这里 - https://gist.github.com/simonsmith/4353587

现在您可以像这样使用该插件:

require(['jquery', 'jquery.plugin'], function($) {
$('.test li').plugin({
test: 'option1',
test2: 'option2'
});
});

对象的实例保存在数据属性中,因此始终可以访问它。 Herotabs使用这种技术:

var instance = $('.tabs').herotabs().data('herotabs');
instance.nextTab();

关于javascript - jQuery 插件 + AMD = 如何访问函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20379641/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com