Reputation: 1
--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
also are there any pitfalls to this method?
Upvotes: 0
Views: 44
Reputation: 22719
You're looking for an understanding of "lexical scope". Here's one explanation: What is lexical scope?
Upvotes: 0