//bootstrap.js
window.numToRubles = require('./custom/rubles');
//in rubles.js
(function() {
var rubles = function(number, template) {
....
return text;
}
globals.rubles = rubles;
})();
它正常工作
//in app.js
numToRubles.rubles(val, {nominal:true, div:true, dat:false});
但如果我在其他电话中改变了一些选项
//in app.js
numToRubles.rubles(val, {nominal:true, div:true, dat:false});
numToRubles.rubles(val, {nominal:true, div:false, dat:true});
它会对第一个值产生影响
我尝试在
中克隆对象 //bootstrap.js
window.numToRubles = require('./custom/rubles');
window.numToRubles2 = window.numToRubles = jQuery.extend(true, {}, window.numToRubles);
//in some.js
numToRubles.rubles(val, {nominal:true, div:true, dat:false});
numToRubles2.rubles(val, {nominal:true, div:false, dat:true});
但它不起作用。它有相同的行为。
现在,我强迫它像这样工作:
window.numToRubles = require('./custom/rubles');
window.numToRubles2 = require('./custom/rubles2');
但这是不好的方式。
我怎么能正确地做到这一点?
答案 0 :(得分:0)
请尝试以下操作:
window.numToRubles = require('./custom/rubles');
window.numToRubles2 = window.numToRubles.bind({});
//clones the function with '{}' acting as it's new 'this' parameter
现在
//in some.js
numToRubles.rubles(val, {nominal:true, div:true, dat:false});
numToRubles2.rubles(val, {nominal:true, div:false, dat:true});
答案 1 :(得分:0)
我将rubles.js改写成了课堂风格
//in rubles.js
(function() {
//var somePar = [....];// and I transferred this property to the class
// I suppose that the manipulation of this array
// led to a negative result before
//add the constructor
var Rubles = function(template) {
var s = this;
s.somePar = [....];
s.getRes(n){
...
return n;
}
}
globals.Rubles = Rubles;
})();
以及何时
window.Rubles = require('./custom/rubles');
window.numToRubles = new window.Rubles.Rubles({nominal:true, div:true, dat:false});
window.numToString = new window.Rubles.Rubles({nominal:false, div:false, dat:true});
res1 = numToRubles.getRes(n);
res2 = numToString.getRes(n);
工作正常