1.new_file.htm
<!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=UTF-8" />
<title>Untitled Document</title>
<SCRIPT type='text/javascript' src='new_file.js'></SCRIPT>
<SCRIPT type='text/javascript'>
window.onload = load;
</SCRIPT>
</head>
<body>
<CENTER><INPUT type='button' id='b1'></INPUT></CENTER>
</body>
</html>
2.new_file.js
var value = '500';
function load(){
var domEl = document.getElementById('b1');
JsButton(value,domEl);
}
function JsButton(value,domEl){
this.domEl = domEl;
this.value = value;
this.domEl.buttonObj = this;
//this.domEl.onclick = function(){
//alert(this.value);
//}
this.domEl.onclick = JsButton.prototype.clickHandler;
}
JsButton.prototype.clickHandler = function(){
//alert(this.value); 在回調函數中調用this屬性時﹐
//得到的是對應的DOM元素裡面的數值﹐此處為空。
var buttonObj = this.buttonObj;
var value =(buttonObj && buttonObj.value)? buttonObj.value:"unknown value";
alert(value);
}