는 사람이 나에게 다음 사항을하시기 바랍니다 설명 할 수 있습니다.자바 스크립트와 jQuery를 루프 배열
사례 1 : 내가 예상대로 작동
function MyObj() {
this.myArray = new Array();
for(var i = 0; i<5; i++){
this.myArray.push(i);
console.log("add to array:" + i);
}
}
. this.myArray는 0, 1, 2, 3, 4를가집니다.
사례 2 :
function MyObj() {
this.myArray = new Array();
$.each([0,1,2,3,4],function(i,v){
this.myArray.push(i);
console.log("add to array:" + v);
});
}
내가 가진은 "this.myArray가 정의되어 있지 않습니다."방화범에서 불평
감사합니다.
. case 2의 "this"는 배열의 실제 숫자입니다.그것은 [0,1,2,3,4]를 참조하고 있습니다. var를 만들고 'this'를 사용하면 안됩니다. 그래서 당신은'function() { myArray.push (this);' – Matt