jQuery插件+AMD=如何访问函数
jQuery plugin + AMD = how to access functions?
我正在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不是函数
console.log($.fn.myPlugin.init());
你知道我做错了什么吗?如何访问myPlugin.prototype = {...}
内部的功能?
编辑:
用这个样板测试
console.log($('.test li').plugin({
test: 'option1',
test2: 'option2'
}));
结果,
Object[] // why is it empty?
和
console.log($.fn.plugin.someMethod());
结果,
TypeError:$.fn.plugin.someMethod不是函数
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为null//这意味着什么?它应该返回"你好世界",不是吗?
instance.someMethod("helloworld");
编辑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不是函数
console.log(instance.someMethod("helloworld"));
您似乎并没有真正创建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();
var plugin = $('.element').plugin();
var instance = $('.element').data('plugin');
console.log(instance);
console.log(instance.someMethod("hello world"));
结果,
Object { element={...}, options={...}, constructor=function(), more...}
hello world
相关文章:
- 访问函数外部的变量
- 从嵌套函数访问函数属性
- 访问函数对象的上下文属性|如何
- jQuery 无法使用 AJAX 调用访问函数内部的函数参数
- 访问函数中的函数名称
- 访问函数'的属性
- 访问函数内部的Polymer方法
- 从另一个文件访问函数的返回值
- 如何从AngularJS外部访问函数
- 如何在 JavaScript 中访问函数中的其他属性值作为对象属性值
- 从require()'d文件访问函数
- 如何访问函数中的变量
- JavaScript 访问函数构造函数中的“this”
- 跨不同作用域访问函数
- 访问函数内部的变量
- jQuery插件+AMD=如何访问函数
- 如何在javascript中访问函数内部的相同变量名
- 可以'无论我做什么,都无法访问函数外的数组
- 如何从module.exports访问函数
- 无法't访问函数中的对象