这是我认为很简单的代码:
<html>
<head>
<script>
function Foobar(id) {
self = this;
self.id = id;
self.canvas = document.createElement('canvas');
self.canvas.style.border = '1px solid black';
document.body.appendChild(self.canvas);
self.canvas.addEventListener('mousedown', self.onMouseDown, true);
self.onMouseDown = function(e) {
console.log(self.id);
}
}
var s1, s2;
function onLoad() {
s1 = new Foobar(1);
s2 = new Foobar(2);
}
</script>
</head>
<body onload='onLoad()'>
</body>
</html>
为什么控制台不弹出id号?
这是一个小提琴:http: //jsfiddle.net/VRn7v/