1
我有下面的代碼,允許我用插件擴展基本的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');
我該如何重新考慮擴展方法來允許這個?
這基本上是我所需要的。我只是將Object.create替換爲polyfill,因爲我需要Ie8支持。謝謝! –