window.onload = function(){
var hatImage = document.getElementById('hatImage');
var button = document.getElementById('changeImage');
this.showRabbit = function(){
hatImage.setAttribute("src", "rabbit-hat.gif");
button.setAttribute("value", "Hide Rabbit");
button.onclick = hideRabbit;
}
this.hideRabbit = function(){
hatImage.setAttribute("src", "topHat.gif");
button.setAttribute("value", "Add a rabbit");
button.onclick = showRabbit;
}
}
加上this在外面就能调用了... 为什么??? 为什么用var showRabbit = fcuntion(){}在外面就调不到.
还有, ,我一直不懂这个 this代表什么? window吗.? 还是document ,高手教我
var hatImage = document.getElementById('hatImage');
var button = document.getElementById('changeImage');
this.showRabbit = function(){
hatImage.setAttribute("src", "rabbit-hat.gif");
button.setAttribute("value", "Hide Rabbit");
button.onclick = hideRabbit;
}
this.hideRabbit = function(){
hatImage.setAttribute("src", "topHat.gif");
button.setAttribute("value", "Add a rabbit");
button.onclick = showRabbit;
}
}
加上this在外面就能调用了... 为什么??? 为什么用var showRabbit = fcuntion(){}在外面就调不到.
还有, ,我一直不懂这个 this代表什么? window吗.? 还是document ,高手教我
<input type='button' onclick="alert(this.tagName);">
</BODY>
window.onload = function(){
this.a="123";
}
alert(a); //报a未定义的错误!
alert(window.a); // 警告框“undefined”
this加方法 结果类似!
以上结果刚刚在IE8、Chorme、FireFox测试。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript">
window.onload = function() {
this.a = function() {
alert('a');
}
b = function() {
alert('b');
}
}
</script>
</head>
<body>
<script type="text/javascript">
try{
a();
b();
} catch(e) {
setTimeout(function(){a();b()}, 1000);
}
</script>
</body>
</html>
1.this."<property or method>"的方式实现
2.classFunction.prototype.[FunctionName]
如果你想外部调用(是不需要var定义的)
function Test()
{
function start(){};
}var test=new Test();
test.start();