from. http://www.digital-web.com/articles/scope_in_javascript/
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Complication - digital-web.com</title>
<script>
function BigComputer(answer){
this.the_answer = answer;
this.ask_question = function(){
alert(this.the_answer)
}
}
Function.prototype.bind = function(obj){
var method = this;
temp = function(){
return method.apply(obj, arguments);
};
return temp;
}
function addhandler(){
var deep_thought = new BigComputer(42), the_button = document.getElementById('thebutton');
the_button.onclick = deep_thought.ask_question.bind(deep_thought);
}
window.onload = addhandler;
</script>
</head>
<body>
<button id="thebutton">
Click Me!
</button>
</body>
</html>
최근 덧글