在VS2015中(我也相信在2013年),我如何让智能感知来识别jquery回调中的对象?在下面的代码(‘测试’函数)中,在非jquery循环中,智能感知知道'person‘是什么,但$.each()回调不知道。
var Person = (function () {
"use strict";
var person = function (name, age) {
this.name = name;
this.age = age;
};
return person;
}());
var People = (function () {
"use strict";
var people = function (lang) {
this.language = lang;
this.population = [];
};
return people;
}());
var test = (function () {
"use strict";
var i, person, people = new People('English');
people.population.push(new Person('joe', 30));
people.population.push(new Person('jane', 31));
// method1 : intellisense works - knows what a 'person' is.
for (i = 0; i < people.population.length; i++) {
person = people.population[i];
person.age++;
}
// method2 : intellisense does not work
$.each(people.population, function (index, person) {
person.age++;
});
person = people.population[0];
alert(person.name + "'s age is now" + person.age);
}());发布于 2015-06-12 00:29:06
使用javascript array.forEach()方法,而不是jquery each(),确实允许智能感知理解数组元素。
people.population.forEach(function (person) {
person.age += 1;
});但这两种方法似乎都比基本的' for‘循环慢得多,所以对于大型数组,我将坚持使用它。
https://stackoverflow.com/questions/30424227
复制相似问题