我能够准确地访问内部描述的动态值,但不能在它内部访问。 (主要是我得到了数组的最后一个值)
for( var i =0 ;i< dynamicValues.length; i++){
(function wrap(dynamicValue){
describe("condition", function(){
// It is logging correct value.
console.log(dynamicValue)
it("should be accessible", function(){
// It is not logging correct value, but logging last value of array.
console.log(dynamicValue);
}
})
}(dynamicValues[i]));
}
&#13;
如何获得相同的环境&#34;内部描述&#34;它&#34;块? (这是我的逻辑的简化版本。我在数组元素的位置使用动态对象)
如果有多种功能,
for( var i =0 ;i< dynamicFunctions.length; i++){
(function wrap(dynamicFunction){
describe("condition", function(){
// It is executing all functions.
dynamicFunction.apply(null)
it("should be accessible", function(){
// It is always executing last function of the array.
dynamicFunction.apply(null);
}
})
}(dynamicFunctions[i]));
}
&#13;
答案 0 :(得分:0)
使用.bind()
:
for(var i = 0, len = dynamicValues; i < len; i++) {
describe('condition', function (dynamicValue) {
it('should be accessible', function (dynamicValue) {
console.log(dynamicValue);
}.bind(null, dynamicValue);
}.bind(null, dynamicValues[i]);
}