允许向插件提供参数
Allow a plugin to be fed parameters
我有以下代码,可以使用插件扩展基本Lib类。插件有自己的上下文,库上下文是由提供的
Lib = function (test,width) {
this.libraryProp = test;
this.width = width;
}
Lib.extend = function(name,plugin) {
this.prototype[name] = function() {
return new plugin(this);
}
}
//The plugin
var myObj = function(lib) {
this.chart = chart;
this.pluginProp = 'plug';
console.log('this library prop = ' + this.chart.libraryProp);
}
//A plugin method
myObj.prototype.ggg = function() {
console.log('plugin prop in plugin prototype ' + this.pluginProp);
console.log(this.chart);
console.log('______________________________________________');
}
//extend the base library
Lib.extend('myObj',myObj)
var p = new Lib('instance 1', 900);
var u = p.myObj();
u.ggg();
var m = new Lib('instance 2',800);
var k = m.myObj();
k.ggg();
工作小提琴:http://jsfiddle.net/pnwLv/2/
这一切都有效,但我目前无法提供任何插件参数,比如:
var u = p.myObj('param1','param2');
如何重新考虑extend方法以允许这种情况发生?
类似的东西?
Lib.extend = function (name, plugin) {
this.prototype[name] = function () {
var args = Array.prototype.slice.call(arguments),
obj = Object.create(plugin.prototype); // Create a new object from the plugin prototype
args.unshift(this); // Add Lib instance as the first argument
plugin.apply(obj, args); // Call the plugin constructor with the new object as the context
return obj; // Return the newly created object
}
}
然后,您可以在插件构造函数中检索这些参数:
var myObj = function(lib, param1, param2) {
this.lib = lib;
this.pluginProp = 'plug';
console.log('this library prop = ' + this.lib.libraryProp);
}
相关文章:
- 我的jQuery插件参数没有正确启动,遇到了问题
- 如何使用jQuery插件参数变量
- r.js评估'text'插件,尽管在'stubModules'参数
- 了解插件代码中$.each内部传递的参数的用法
- 如何在jRate插件上设置readOnly;单击一颗星后的参数
- 从插件向Datatables添加额外的服务器参数
- 允许向插件提供参数
- json格式作为cordova或phonegap ios插件的参数
- 它是如何在cordova插件对话框中使用参数按钮Index的
- jQuery插件回调函数参数
- Backbone.js的Backbone.Modelbinding插件中是否有绑定的转换器参数
- 调用插件外部的自定义函数并传递参数
- 动态更改 JQuery 插件中的高度参数
- 设置后如何在 jquery 插件上更新参数
- JQuery:将参数传递给插件以限制代码执行
- 尝试更改 JQuery 插件文件中的参数
- 使用位于插件文件夹内的参数执行二进制文件
- 可更改的属性作为 jQuery 插件中的参数
- 确定 JS 插件未收到必需参数时死亡的最佳实践
- 更改WordPress视频插件以自动设置youtube iframe参数