我在项目中的 PhpStorm 中的 JavaScript 文件中看到警告,我想对此采取措施。我有这样的代码:
function someFunction() {
...
var myArray = [1, 2, 3, ...];
$.each(myArray, function() { someOtherFunction(this); }); //warning on this line
....
}
/**
* @param {Number} value
*/
function someOtherFunction(value) {
...
}
在代码中指示的行上,PHP 给出了以下警告:
Argument type someFunction is not assignable to parameter type Number
我确实理解它的含义以及为什么给出此警告:PhpStorm 无法知道 jQuery 在 .each
循环回调中重新定义了 this
上下文。我的问题是我可以做什么来暗示这种重新定义正在发生以及在这种情况下 this
到底是什么?
最佳答案
function someFunction() {
...
var myArray = [1, 2, 3, ...];
$.each(myArray, function(i, val) { someOtherFunction(val); }); //warning on this line
....
}
http://api.jquery.com/jquery.each/ 这里的 jQuery 文档显示回调函数的参数是实际处理值的参数。传递的第一个参数是数组索引,第二个参数传递的是值,这就是您要查找的值。
问题是this
永远不会只是一个原始值。 jQuery 总是将其包装为一个对象。
关于javascript - PhpStorm警告: argument type not assignable to parameter type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36704073/