2017-05-25 31 views
0

我目前建立一个“纯JS-页”的摆弄周围(无HTML内容没有CSS只是JSON喂养“构造”),并偶然发现我无法解决有关问题是什么我相信关心功能的范围?!的Javascript:事件对象引用

<html> 
 
<head> 
 
<script> 
 
'use strict' 
 
var x = class { 
 
\t constructor(args) { 
 
\t \t console.log('class') 
 
\t \t this.a = args[0] 
 
\t \t this.b = args[1] 
 
\t \t this.c = args[2] 
 
\t \t this.d = this.d.bind(this) 
 
\t \t this.e = this.e.bind(this) 
 
\t \t this.d() 
 
\t } 
 
} 
 
x.prototype.d = function() { 
 
\t console.log('listener') 
 
\t document.getElementById('h').addEventListener('mouseover',this.e,false) 
 
} 
 
x.prototype.e = function() { 
 
\t console.log('handler') 
 
\t this.a(this.b) 
 
} 
 
var f = function(args) { 
 
\t console.log('call: '+args) 
 
\t console.log(event.target) 
 
} 
 
var g = function(args) { 
 
\t new x(args) 
 
} 
 
</script> 
 
</head> 
 
<body onload="g([f,'hello',1000]);"> 
 
<div id="h" style="width:200px;height:200px;background:grey;">lorem...</div> 
 
</body> 
 
</html>

虽然上述代码完全执行作为“预期的”(我不需要单独传递事件对象),我无法实现该事件对象正被传递的setTimeout 。

<html> 
 
<head> 
 
<script> 
 
'use strict' 
 
var x = class { 
 
\t constructor(args) { 
 
\t \t console.log('class') 
 
\t \t this.a = args[0] 
 
\t \t this.b = args[1] 
 
\t \t this.c = args[2] 
 
\t \t this.d = this.d.bind(this) 
 
\t \t this.e = this.e.bind(this) 
 
\t \t this.d() 
 
\t } 
 
} 
 
x.prototype.d = function() { 
 
\t console.log('listener') 
 
\t document.getElementById('h').addEventListener('mouseover',this.e,false) 
 
} 
 
x.prototype.e = function() { 
 
\t console.log('handler') 
 
\t setTimeout(() => {this.a(this.b)},this.c) 
 
} 
 
var f = function(args) { 
 
\t console.log('call: '+args) 
 
\t console.log(event.target) 
 
} 
 
var g = function(args) { 
 
\t new x(args) 
 
} 
 
</script> 
 
</head> 
 
<body onload="g([f,'hello',1000]);"> 
 
<div id="h" style="width:200px;height:200px;background:grey;">lorem...</div> 
 
</body> 
 
</html>

箭头功能幸运的是摆脱对本完整的参考废话,但我失去的“函数f”事件引用。

有什么方法来达到同样的“状态”为上部的例子吗?

任何帮助都非常感谢,但请注意我是一个业余爱好者(是的,我做了“谷歌”),并通过范围阅读后小时,申请,绑定,电话,箭头功能(我希望他们会帮助, ...好)和其他的东西,我仍然无法找到一个妥善的解决办法(!jQuery的& &!节点& &!角& &!打字稿& &香草)。

问候

+0

功能'F'不知道event'是什么'。你的第一个片段是在Chrome中工作,但不是在FireFox中。不要依赖一些浏览器魔法。在你的回调函数中指定'event'作为参数是标准的事情。 – Mako

回答

0

好像你不及格事件参数this.a(e, this.b)}。请参阅更新的片段:

<html> 
 
<head> 
 
<script> 
 
'use strict' 
 
var x = class { 
 
\t constructor(args) { 
 
\t \t console.log('class') 
 
\t \t this.a = args[0] 
 
\t \t this.b = args[1] 
 
\t \t this.c = args[2] 
 
\t \t this.d = this.d.bind(this) 
 
\t \t this.e = this.e.bind(this) 
 
\t \t this.d() 
 
\t } 
 
} 
 
x.prototype.d = function() { 
 
\t console.log('listener') 
 
\t document.getElementById('h').addEventListener('mouseover',this.e,false) 
 
} 
 
x.prototype.e = function(e) { 
 
\t console.log('handler') 
 
\t setTimeout(() => {this.a(e, this.b)},this.c) 
 
} 
 
var f = function(event, args) { 
 
\t console.log('call: '+args) 
 
\t console.log(event.target) 
 
} 
 
var g = function(args) { 
 
\t new x(args) 
 
} 
 
</script> 
 
</head> 
 
<body onload="g([f,'hello',1000]);"> 
 
<div id="h" style="width:200px;height:200px;background:grey;">lorem...</div> 
 
</body> 
 
</html>