JavaScript中的對象化編程
更新時間:2008年01月16日 20:45:22 作者:
JavaScript中的對象化編程
關(guān)于對象化編程的語句 現(xiàn)在我們有實力學(xué)習(xí)以下關(guān)于對象化編程,但其實屬于上一章的內(nèi)容了。
with 語句 為一個或一組語句指定默認(rèn)對象。
用法:
with (<對象>) <語句>;
with 語句通常用來縮短特定情形下必須寫的代碼量。在下面的例子中,請注意 Math 的重復(fù)使用:
x = Math.cos(3 * Math.PI) + Math.sin(Math.LN10);
y = Math.tan(14 * Math.E);
當(dāng)使用 with 語句時,代碼變得更短且更易讀:
with (Math) {
x = cos(3 * PI) + sin(LN10);
y = tan(14 * E);
}
this 對象 返回“當(dāng)前”對象。在不同的地方,this 代表不同的對象。如果在 JavaScript 的“主程序”中(不在任何 function 中,不在任何事件處理程序中)使用 this,它就代表 window 對象;如果在 with 語句塊中使用 this,它就代表 with 所指定的對象;如果在事件處理程序中使用 this,它就代表發(fā)生事件的對象。
一個常用的 this 用法:
<script>
...
function check(formObj) {
...
}
...
</script>
<body ...>
...
<form ...>
...
<input type="text" ... onchange="check(this.form)">
...
</form>
...
</body>
這個用法常用于立刻檢測表單輸入的有效性。
自定義構(gòu)造函數(shù) 我們已經(jīng)知道,Array(),Image()等構(gòu)造函數(shù)能讓我們構(gòu)造一個變量。其實我們自己也可以寫自己的構(gòu)造函數(shù)。自定義構(gòu)造函數(shù)也是用 function。在 function 里邊用 this 來定義屬性。
function <構(gòu)造函數(shù)名> [(<參數(shù)>)] {
...
this.<屬性名> = <初始值>;
...
}
然后,用 new 構(gòu)造函數(shù)關(guān)鍵字來構(gòu)造變量:
var <變量名> = new <構(gòu)造函數(shù)名>[(<參數(shù)>)];
構(gòu)造變量以后,<變量名>成為一個對象,它有它自己的屬性——用 this 在 function 里設(shè)定的屬性。
以下是一個從網(wǎng)上找到的搜集瀏覽器詳細(xì)資料的自定義構(gòu)造函數(shù)的例子:
function Is() {
var agent = navigator.userAgent.toLowerCase();
this.major = parseInt(navigator.appVersion); //主版本號
this.minor = parseFloat(navigator.appVersion);//全版本號
this.ns = ((agent.indexOf('mozilla')!=-1) &&
((agent.indexOf('spoofer')==-1) && //是否 Netscape
(agent.indexOf('compatible') == -1)));
this.ns2 = (this.ns && (this.major == 3)); //是否 Netscape 2
this.ns3 = (this.ns && (this.major == 3)); //是否 Netscape 3
this.ns4b = (this.ns && (this.minor < 4.04)); //是否 Netscape 4 低版本
this.ns4 = (this.ns && (this.major >= 4)); //是否 Netscape 4 高版本
this.ie = (agent.indexOf("msie") != -1); //是否 IE
this.ie3 = (this.ie && (this.major == 2)); //是否 IE 3
this.ie4 = (this.ie && (this.major >= 4)); //是否 IE 4
this.op3 = (agent.indexOf("opera") != -1); //是否 Opera 3
this.win = (agent.indexOf("win")!=-1); //是否 Windows 版本
this.mac = (agent.indexOf("mac")!=-1); //是否 Macintosh 版本
this.unix = (agent.indexOf("x11")!=-1); //是否 Unix 版本
}
var is = new Is();
這個構(gòu)造函數(shù)非常完整的搜集了瀏覽器的信息。我們看到它為對象定義了很多個屬性:major, minor, ns, ie, win, mac 等等。它們的意思見上面的注釋。把 is 變量定義為 Is() 對象后,用 if (is.ns) 這種格式就可以很方便的知道瀏覽器的信息了。我們也可以從這個構(gòu)造函數(shù)中看到,它也可以使用一般的 JavaScript 語句(上例中為 var 語句)。
讓我們再來看一個使用參數(shù)的構(gòu)造函數(shù):
function myFriend(theName, gender, theAge, birthOn, theJob) {
this.name = theName;
this.isMale = (gender.toLowerCase == 'male');
this.age = theAge;
this.birthday = new Date(birthOn);
this.job = theJob
}
var Stephen = new myFriend('Stephen', 'Male', 18, 'Dec 22, 1982', 'Student');
從這個構(gòu)造函數(shù)我們不但看到了參數(shù)的用法,還看到了不同的屬性用不同的數(shù)據(jù)型是可以的(上例五個屬性分別為:字符串,布爾值,數(shù)字,日期,字符串),還看到了構(gòu)造函數(shù)里也可以用構(gòu)造函數(shù)來“構(gòu)造”屬性。如果用了足夠的“保護(hù)措施”來避免無限循環(huán),更可以用構(gòu)造函數(shù)自身來構(gòu)造自己的屬性。
with 語句 為一個或一組語句指定默認(rèn)對象。
用法:
with (<對象>) <語句>;
with 語句通常用來縮短特定情形下必須寫的代碼量。在下面的例子中,請注意 Math 的重復(fù)使用:
x = Math.cos(3 * Math.PI) + Math.sin(Math.LN10);
y = Math.tan(14 * Math.E);
當(dāng)使用 with 語句時,代碼變得更短且更易讀:
with (Math) {
x = cos(3 * PI) + sin(LN10);
y = tan(14 * E);
}
this 對象 返回“當(dāng)前”對象。在不同的地方,this 代表不同的對象。如果在 JavaScript 的“主程序”中(不在任何 function 中,不在任何事件處理程序中)使用 this,它就代表 window 對象;如果在 with 語句塊中使用 this,它就代表 with 所指定的對象;如果在事件處理程序中使用 this,它就代表發(fā)生事件的對象。
一個常用的 this 用法:
<script>
...
function check(formObj) {
...
}
...
</script>
<body ...>
...
<form ...>
...
<input type="text" ... onchange="check(this.form)">
...
</form>
...
</body>
這個用法常用于立刻檢測表單輸入的有效性。
自定義構(gòu)造函數(shù) 我們已經(jīng)知道,Array(),Image()等構(gòu)造函數(shù)能讓我們構(gòu)造一個變量。其實我們自己也可以寫自己的構(gòu)造函數(shù)。自定義構(gòu)造函數(shù)也是用 function。在 function 里邊用 this 來定義屬性。
function <構(gòu)造函數(shù)名> [(<參數(shù)>)] {
...
this.<屬性名> = <初始值>;
...
}
然后,用 new 構(gòu)造函數(shù)關(guān)鍵字來構(gòu)造變量:
var <變量名> = new <構(gòu)造函數(shù)名>[(<參數(shù)>)];
構(gòu)造變量以后,<變量名>成為一個對象,它有它自己的屬性——用 this 在 function 里設(shè)定的屬性。
以下是一個從網(wǎng)上找到的搜集瀏覽器詳細(xì)資料的自定義構(gòu)造函數(shù)的例子:
function Is() {
var agent = navigator.userAgent.toLowerCase();
this.major = parseInt(navigator.appVersion); //主版本號
this.minor = parseFloat(navigator.appVersion);//全版本號
this.ns = ((agent.indexOf('mozilla')!=-1) &&
((agent.indexOf('spoofer')==-1) && //是否 Netscape
(agent.indexOf('compatible') == -1)));
this.ns2 = (this.ns && (this.major == 3)); //是否 Netscape 2
this.ns3 = (this.ns && (this.major == 3)); //是否 Netscape 3
this.ns4b = (this.ns && (this.minor < 4.04)); //是否 Netscape 4 低版本
this.ns4 = (this.ns && (this.major >= 4)); //是否 Netscape 4 高版本
this.ie = (agent.indexOf("msie") != -1); //是否 IE
this.ie3 = (this.ie && (this.major == 2)); //是否 IE 3
this.ie4 = (this.ie && (this.major >= 4)); //是否 IE 4
this.op3 = (agent.indexOf("opera") != -1); //是否 Opera 3
this.win = (agent.indexOf("win")!=-1); //是否 Windows 版本
this.mac = (agent.indexOf("mac")!=-1); //是否 Macintosh 版本
this.unix = (agent.indexOf("x11")!=-1); //是否 Unix 版本
}
var is = new Is();
這個構(gòu)造函數(shù)非常完整的搜集了瀏覽器的信息。我們看到它為對象定義了很多個屬性:major, minor, ns, ie, win, mac 等等。它們的意思見上面的注釋。把 is 變量定義為 Is() 對象后,用 if (is.ns) 這種格式就可以很方便的知道瀏覽器的信息了。我們也可以從這個構(gòu)造函數(shù)中看到,它也可以使用一般的 JavaScript 語句(上例中為 var 語句)。
讓我們再來看一個使用參數(shù)的構(gòu)造函數(shù):
function myFriend(theName, gender, theAge, birthOn, theJob) {
this.name = theName;
this.isMale = (gender.toLowerCase == 'male');
this.age = theAge;
this.birthday = new Date(birthOn);
this.job = theJob
}
var Stephen = new myFriend('Stephen', 'Male', 18, 'Dec 22, 1982', 'Student');
從這個構(gòu)造函數(shù)我們不但看到了參數(shù)的用法,還看到了不同的屬性用不同的數(shù)據(jù)型是可以的(上例五個屬性分別為:字符串,布爾值,數(shù)字,日期,字符串),還看到了構(gòu)造函數(shù)里也可以用構(gòu)造函數(shù)來“構(gòu)造”屬性。如果用了足夠的“保護(hù)措施”來避免無限循環(huán),更可以用構(gòu)造函數(shù)自身來構(gòu)造自己的屬性。
您可能感興趣的文章:
相關(guān)文章
Typescript中interface與type的相同點與不同點的詳細(xì)說明
這篇文章主要介紹了Typescript中interface與type的相同點與不同點,并配有實例說明,需要的朋友可以參考下2022-11-11在Javascript中處理數(shù)組之toSource()方法的使用
這篇文章主要介紹了在Javascript中處理數(shù)組之toSource()方法的使用,是JS入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下2015-06-06uniapp父子組件傳值3種方法(props、slot和ref)
這篇文章主要給大家介紹了關(guān)于uniapp父子組件傳值的3種方法,方法包括props、slot和ref,最近看到uniapp組件傳值的方法,這里記錄一下,需要的朋友可以參考下2023-07-07鍵盤 keycode的值 javascript時觸發(fā)事件時很有用的要素
鍵盤keycode的值 編寫javascript時觸發(fā)事件時很有用的要素,大家可以收藏一下。2009-11-11