js面向(基於)對象編程——類(原型對象)與對象
this—問題提出
請大家先看一段代碼:
<html>
<head>
<script language="javascript">
function Person(){
}
var p1=new Person();
p1.name="順平";
p1.age=60;
window.alert(p1.name+" "+p1.age);
var p2=new Person();
window.alert(p2.name); //這裏會輸出什麼?(undefined)
</script>
</head>
<body></body>
</html>
解決之道:使用this
js虛擬機會給每一個對象分配this,代表當前對象。坦白的講,要明白this(我的)不是件容易的事情,給大家打一個比方(上帝造人小故事)
function Person(){
this.name="abc"
this.age=90;
}
var p1=new Person();
var p2=new Person();
p2.name="順平";
window.alert(p1.name+" "+p2.name); //輸出 abc 順平
<html>
<head>
<script language="javascript">
function Person(){
var name="abc"; //如果這樣去使用 name這個屬性是私有的
var age = 90;
this.name2="abc2"; //this.name2 加this表示name2這個屬性是公開的
//this.show()就是Person的一個公開方法
//如果你一定要訪問私有屬性,則需要定義一個公開方法(特權方法)
this.show=function(){
window.alert(age+" "+name); //name屬性是私有的
}
//show2()這個是私有方法,只能在Person類中使用
//如果想在外部使用show2,可以在this.show中調用
//javascript中大部分方法都是公開的
function show2(){
window.alert("show2()"+age+" "+name);
}
}
var p1=new Person();
//這樣調用name屬性,會報undefined
window.alert(p1.name);
window.alert(Person.name); //這個輸出什麼? 輸出 Person
//name2屬性是公開的
window.alert(p1.name2); //輸出 abc2
//如果要訪問私有屬性name,就要在類中定義一個公開的方法,比如定義一個show方法
p1.show(); //輸出90 abc
p1.show2(); //這裏會報錯
</script>
</head>
<body><body>
</html>
爲了進一步理解this,再看一個程序
<html>
<head>
<script language="javascript">
function test1(){
alert(this.v);
}
var v=90;
test1(); //test1()這個函數,從面向對象的角度看,它應該是屬於某個對象的,它就是屬於window的
window.test1();// 輸出什麼? 等價於執行 test1();
// alert("ok"); 等同於 window.alert("ok");
//哪個對象實例調用this所在的函數,那麼this就代表哪個對象實例
//window.test1(); this就是window,進去了以後,調test1,
//如果test1裏面有this,那個這個this就是調用者window,
//這個this有沒有v呢,在外面定義了一個v=90,window對象是全局對象。
window.alert(v); //輸出v
window.alert(window.v) //同樣輸出的v,和上面的一樣的
//window對象是全局對象,在上面的test1函數中alert(this.v);,
//不就是等同於alert(window.v);嗎,這樣就容易理解了
//什麼叫當前對象是this呢
function Person(){
this.abc=function(){
window.alert(this.v); //這裏的this就是p,誰調用this,this就是誰
}
}
var p=new Person();
p.v="hello"; //動態的
p.abc(); //p調用abc,p就是裏面的this,而this確實有v這個屬性,則hello就被輸出了
</script>
</head>
<body></body>
</html>
window.alert("ok");這兩個是一個意思,alert這個函數默認就是window對象。
test1(); 這個就是前面沒有寫window而已,它和下面的是一樣的。
window.test1();
javascript中的 HTML DOM window對象
window對象是javascript層級中的頂級對象。
window對象代表一個瀏覽器窗口或一個框架
window對象會在<body>或<frameset>每次出現時被自動創建
window對象表示一個瀏覽器窗口或一個框架。在客戶端javascript中,window對象是全局對象,所有的表達式都在當前的環境中計算。也就是說,要引用當前窗口根本不需要特殊的語法,可以把那個窗口的屬性作爲全局變量來使用。例如,可以只寫document,而不必寫window.document。
同樣,可以把當前窗口對象的方法當做函數來使用,如可只寫alert(),而不必寫window.alert()。
記住一句話:
哪個對象實例調用this所在的函數,那麼this就代表哪個對象實例。
this——注意事項
this不能在類定義的外部使用,只能在類定義的方法中使用。在類定義的外部,調用者就變成window了
<html>
<head>
<script language="javascript">
function Person(){
this.abc=function(){
window.alert(this.v); //這裏的this就是p,誰調用this,this就是誰
}
}
var p=new Person();
p.v="hello"; //動態的
p.abc(); //p調用abc,p就是裏面的this,而this確實有v這個屬性,則hello就被輸出了
window.alert(this.v); //在類定義的外部這樣使用,此時的this指的是window了,就變成另外一個對象了,
//它並會把p.v的hello輸出,因爲在類的外部這個this已經指的是window了
</script>
</head>
<body></body>
</html>