我正在尝试动态生成mocha测试,但是我遇到了一些问题:
expect([1, 2, 3])['to']['deep']['equal']([1, 2, 3]);然而,工作正常。
var e = expect([1, 2, 3]);
e = e['to'];
e = e['deep'];
e = e['equal'];
e([1, 2, 3]);`产生
Uncaught TypeError: this.assert is not a function at assertEqual (node_modules/chai/lib/chai/core/assertions.js:487:12) at ctx.(anonymous function) (node_modules/chai/lib/chai/utils/addMethod.js:41:25)
在e([1, 2, 3]);上。你知道这里出了什么问题吗?或者我该如何解决这个问题?
发布于 2016-06-04 06:09:24
默认情况下,不绑定JavaScript方法。
var a = {whoAmI: 'a', method: function() {console.log(this);}}
var b = {whoAmI: 'b'};
console.log(a.method()); // will print a
var method = a.method;
method(); // will print the global object (Window)
b.method = method;
b.method(); // will print b如果你需要绑定,你可以使用闭包:
// simple case
var method = function() {return a.method();}
// slightly more complex case, supporting arguments
var method = function() {return a.method.apply(a, arguments);}
method(); // will print a
b.method = method;
b.method(); // will still print a或者,您可以使用内置的.bind()方法。
var method = a.method.bind(a);https://stackoverflow.com/questions/37624081
复制相似问题