function SomeFunction(arg){ var self = this; self.module1 = function(){ return { moduleFunc: function(){ console.log(val); } }; } self.module2 = function(){ return { moduleFunc: function(){ console.log(val); } }; } self.base = function(){ var val; return { getValue: function(){ return val; }, setValue: function(arg){ val = arg; } } } //下面省掉的代码可以保证通过传入arg来确定执行module1还是module2 } var module1 = SomeFunction("module1"); module1.setValue("xxxx"); console.log(module1.getValue());//期望打印xxxx module1.moduleFunc();//期望也打印xxxx
现在希望module1和module2执行后返回的object中有base中getValue和setValue两个方法,
同时保证moduleFunc可以访问到base中的val
而且允许module1和module2中使用自己的setValue和getValue覆盖base里默认提供的方法
如何做到继承或者覆盖呢?
可以使用jQuery.extend如果需要的话
module2也是一样。