javascript 面向對象編程 萬物皆對象
更新時間:2009年09月17日 23:22:45 作者:
javascript幾乎成了如今web開發(fā)人員必學必會的一門語言,但很多人卻只停在了一些表單驗證等基礎操作層面上,在面向對象語言大行其道的當下,我們需要去學習javascript的面向對象的知識,以便更好的掌握javascript、為深入理解各種腳本框架打好基礎。
javascript和java、C#等語言一樣也具有面向對象的一些特征,但細比較的時候,會發(fā)現(xiàn)這些特征并不是真正的面向對象,很多地方都是利用對象本身來模擬面向對象,所以認為javascript不能算是面向對象編程語言,而是基于對象的語言。
在javascript中真的是萬物皆對象,new出來的東西是對象,方法是對象,連類也都是對象。下面分別來看一下對象、方法和類的對象特征。
1.拿內置的Date來看一下吧
var time = new Date();
var timeString = time.getFullYear() + "-" +
time.getMonth() + "-" +
time.getDate() + " " +
time.getHours() + ":" +
time.getMinutes() + ":" +
time.getSeconds();
document.write(timeString);
通過 time來操作其所引用的Date對象,可以方便的調用Date的對象所包含的一系列getXX()方法來獲取年月日時分秒等信息。
可以再看一下String
var username = new String("hello world");
document.write(username.length);
變量username引用了new出來的字符串對象,通過username訪問字符串對象的length屬性。
2.方法也是對象
function hello() {
alert("hello");
};
var helloRef = hello;
helloRef();
hello是一個方法,helloRef是一個引用了hello方法的變量,helloRef和hello一樣都指向了相同的方法對象。也就意味著helloRef也可以執(zhí)行,helloRef()。同理也可以寫出以下代碼。
var helloRef = function() {
alert("hello");
};
helloRef();
function(){alert(“hello”)}是一個匿名方法,當然也是對象,用helloRef變量引用該方法對象后,可以通過helloRef來調用方法。
3.那么類呢?當然類也是對象,在javascript中,不像C#或java那樣有class關鍵字用來創(chuàng)建類,而是直接使用方法的關鍵字來創(chuàng)建類或者叫模擬類。
function Person(username, age) {
this.Name = username;
this.Age = age;
this.Introduce = function() {
alert("我叫" + this.Name + ",今年" + this.Age + "歲了。");
};
};
var person1 = new Person("張三", 20);
person1.Introduce();
以上創(chuàng)建了一個Person類型,Person帶有構造參數(shù)username和age,通過創(chuàng)建的Person對象可以調用Person所包含的方法Introduce。下面對代碼做一些修改。
function Person(username, age) {
this.Name = username;
this.Age = age;
this.Introduce = function() {
alert("我叫" + this.Name + ",今年" + this.Age + "歲了。");
};
};
var PersonClass = Person;
var person1 = new PersonClass("張三", 20);
person1.Introduce();
重新聲明新的變量PersonClass并引用Person類,PersonClass和Person都指向了原來的Person所引用的類,所以也可以用PersonClass來創(chuàng)建對象。
以上的幾個例子可能不是很恰當,但也可以一窺javascript中萬物皆對象。
下一節(jié)詳細的談一談javascript中的對象。
在javascript中真的是萬物皆對象,new出來的東西是對象,方法是對象,連類也都是對象。下面分別來看一下對象、方法和類的對象特征。
1.拿內置的Date來看一下吧
復制代碼 代碼如下:
var time = new Date();
var timeString = time.getFullYear() + "-" +
time.getMonth() + "-" +
time.getDate() + " " +
time.getHours() + ":" +
time.getMinutes() + ":" +
time.getSeconds();
document.write(timeString);
通過 time來操作其所引用的Date對象,可以方便的調用Date的對象所包含的一系列getXX()方法來獲取年月日時分秒等信息。
可以再看一下String
復制代碼 代碼如下:
var username = new String("hello world");
document.write(username.length);
變量username引用了new出來的字符串對象,通過username訪問字符串對象的length屬性。
2.方法也是對象
復制代碼 代碼如下:
function hello() {
alert("hello");
};
var helloRef = hello;
helloRef();
hello是一個方法,helloRef是一個引用了hello方法的變量,helloRef和hello一樣都指向了相同的方法對象。也就意味著helloRef也可以執(zhí)行,helloRef()。同理也可以寫出以下代碼。
復制代碼 代碼如下:
var helloRef = function() {
alert("hello");
};
helloRef();
function(){alert(“hello”)}是一個匿名方法,當然也是對象,用helloRef變量引用該方法對象后,可以通過helloRef來調用方法。
3.那么類呢?當然類也是對象,在javascript中,不像C#或java那樣有class關鍵字用來創(chuàng)建類,而是直接使用方法的關鍵字來創(chuàng)建類或者叫模擬類。
復制代碼 代碼如下:
function Person(username, age) {
this.Name = username;
this.Age = age;
this.Introduce = function() {
alert("我叫" + this.Name + ",今年" + this.Age + "歲了。");
};
};
var person1 = new Person("張三", 20);
person1.Introduce();
以上創(chuàng)建了一個Person類型,Person帶有構造參數(shù)username和age,通過創(chuàng)建的Person對象可以調用Person所包含的方法Introduce。下面對代碼做一些修改。
復制代碼 代碼如下:
function Person(username, age) {
this.Name = username;
this.Age = age;
this.Introduce = function() {
alert("我叫" + this.Name + ",今年" + this.Age + "歲了。");
};
};
var PersonClass = Person;
var person1 = new PersonClass("張三", 20);
person1.Introduce();
重新聲明新的變量PersonClass并引用Person類,PersonClass和Person都指向了原來的Person所引用的類,所以也可以用PersonClass來創(chuàng)建對象。
以上的幾個例子可能不是很恰當,但也可以一窺javascript中萬物皆對象。
下一節(jié)詳細的談一談javascript中的對象。
相關文章
javascript實現(xiàn)面向對象類的功能書寫技巧
經過前段時間,學習《ajax完全自學手冊》后,才知道javascript原來還可以這么寫。2010-03-03javascript 面向對象編程 function是方法(函數(shù))
在進行編程時,必免不了要碰到復雜的功能。初學者最怕復雜的功能,因為不能夠很好的進行功能邊界劃分,只能一大串if、循環(huán)加case堆疊在一起,結果出來的程序自己看著暈,別人看著更暈。2009-09-09