[英]can someone tell me how/why this works in ff and ie
--script--
function myalert(name){
var obj=this;
obj.run=run;
function run(){
alert(name);
}
div=document.createElement('div');
div=document.body.append(div);
txt=document.createTextNode('alert');
div.appendChild(txt);
//**
div.onclick=function(){ obj.run(); }
//**
}
--html--
<div onclick="myalert('mee');">matt</div>
the thing i find strange, is the obj.run() is called outside the function, from a dom element, and the function parameter is also preserved 我发现很奇怪的是,从dom元素中在函数外部调用了obj.run(),并且还保留了函数参数
also are there any pitfalls to this method? 这个方法还有什么陷阱吗?
This is called Closure . 这称为Closure 。
There is a lot to read on this. 关于这一点有很多值得阅读的内容。
You're looking for an understanding of "lexical scope". 您正在寻找对“词法范围”的理解。 Here's one explanation: What is lexical scope?
这是一种解释: 什么是词汇范围?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.