这是我正在尝试做的事情的简单版本:
<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 的位置两次,
为什么?
最佳答案
关于javascript - javascript onload 如何在实例中工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34571676/