这是我试图做的简单版本:
<script type="text/javascript">
function Example(position) {
this.position = position;
instance = this;
img = new Image();
img.src = 'http://www.gazette-ariegeoise.fr/IMG/jpg/test.jpg';
console.log(this.position);
//logs as expected 1 than 2 on the second
img.onload = function() {
console.log(instance.position);
//logs 2 twice
}
}
ex1 = new Example(1);
ex2 = new Example(2);
</script>
以某种方式onload函数记录实例2的位置两次,
为什么?
答案 0 :(得分:4)