【发布时间】:2013-12-21 11:51:51
【问题描述】:
我想模块化一些功能,我想使用下划线js之类的模式,但我总是收到全局泄漏警告。
// simple test use case
var decorate = require('../lib/decorate');
var expect = require('expect.js');
describe('decorate', function() {
it('should wrap', function() {
var arr = []
expect( decorate('dummy').wrapWith(' oOo ') ).to.eql( ' oOo dummy oOo ' );
});
});
现在它通过mocha --ignore-leaks 传递,但可以在没有全局泄漏的情况下包装它吗?
这是基本代码:
// decorate.js
(function () {
// 'use strict'; // not yet
function Decorate(obj) {
this._obj = obj; // FIXME: global leaks _obj
if (!(this instanceof Decorate)){
return new Decorate(this._obj);
}
}
Decorate.prototype.wrapWith = function(wrap) {
return wrap + this._obj + wrap;
}
// export for node or the browser
if (typeof module !== 'undefined') {
module.exports = Decorate;
} else {
window.decorate = Decorate;
}
}.call(this));
【问题讨论】:
标签: javascript design-patterns refactoring scope mocha.js