javascript 面向?qū)ο缶幊?萬(wàn)物皆對(duì)象
更新時(shí)間:2009年09月17日 23:22:45 作者:
javascript幾乎成了如今web開(kāi)發(fā)人員必學(xué)必會(huì)的一門(mén)語(yǔ)言,但很多人卻只停在了一些表單驗(yàn)證等基礎(chǔ)操作層面上,在面向?qū)ο笳Z(yǔ)言大行其道的當(dāng)下,我們需要去學(xué)習(xí)javascript的面向?qū)ο蟮闹R(shí),以便更好的掌握javascript、為深入理解各種腳本框架打好基礎(chǔ)。
javascript和java、C#等語(yǔ)言一樣也具有面向?qū)ο蟮囊恍┨卣?,但?xì)比較的時(shí)候,會(huì)發(fā)現(xiàn)這些特征并不是真正的面向?qū)ο?,很多地方都是利用?duì)象本身來(lái)模擬面向?qū)ο螅哉J(rèn)為javascript不能算是面向?qū)ο缶幊陶Z(yǔ)言,而是基于對(duì)象的語(yǔ)言。
在javascript中真的是萬(wàn)物皆對(duì)象,new出來(lái)的東西是對(duì)象,方法是對(duì)象,連類(lèi)也都是對(duì)象。下面分別來(lái)看一下對(duì)象、方法和類(lèi)的對(duì)象特征。
1.拿內(nèi)置的Date來(lái)看一下吧
var time = new Date();
var timeString = time.getFullYear() + "-" +
time.getMonth() + "-" +
time.getDate() + " " +
time.getHours() + ":" +
time.getMinutes() + ":" +
time.getSeconds();
document.write(timeString);
通過(guò) time來(lái)操作其所引用的Date對(duì)象,可以方便的調(diào)用Date的對(duì)象所包含的一系列g(shù)etXX()方法來(lái)獲取年月日時(shí)分秒等信息。
可以再看一下String
var username = new String("hello world");
document.write(username.length);
變量username引用了new出來(lái)的字符串對(duì)象,通過(guò)username訪問(wèn)字符串對(duì)象的length屬性。
2.方法也是對(duì)象
function hello() {
alert("hello");
};
var helloRef = hello;
helloRef();
hello是一個(gè)方法,helloRef是一個(gè)引用了hello方法的變量,helloRef和hello一樣都指向了相同的方法對(duì)象。也就意味著helloRef也可以執(zhí)行,helloRef()。同理也可以寫(xiě)出以下代碼。
var helloRef = function() {
alert("hello");
};
helloRef();
function(){alert(“hello”)}是一個(gè)匿名方法,當(dāng)然也是對(duì)象,用helloRef變量引用該方法對(duì)象后,可以通過(guò)helloRef來(lái)調(diào)用方法。
3.那么類(lèi)呢?當(dāng)然類(lèi)也是對(duì)象,在javascript中,不像C#或java那樣有class關(guān)鍵字用來(lái)創(chuàng)建類(lèi),而是直接使用方法的關(guān)鍵字來(lái)創(chuàng)建類(lèi)或者叫模擬類(lèi)。
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)建了一個(gè)Person類(lèi)型,Person帶有構(gòu)造參數(shù)username和age,通過(guò)創(chuàng)建的Person對(duì)象可以調(diào)用Person所包含的方法Introduce。下面對(duì)代碼做一些修改。
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類(lèi),PersonClass和Person都指向了原來(lái)的Person所引用的類(lèi),所以也可以用PersonClass來(lái)創(chuàng)建對(duì)象。
以上的幾個(gè)例子可能不是很恰當(dāng),但也可以一窺javascript中萬(wàn)物皆對(duì)象。
下一節(jié)詳細(xì)的談一談javascript中的對(duì)象。
在javascript中真的是萬(wàn)物皆對(duì)象,new出來(lái)的東西是對(duì)象,方法是對(duì)象,連類(lèi)也都是對(duì)象。下面分別來(lái)看一下對(duì)象、方法和類(lèi)的對(duì)象特征。
1.拿內(nèi)置的Date來(lái)看一下吧
復(fù)制代碼 代碼如下:
var time = new Date();
var timeString = time.getFullYear() + "-" +
time.getMonth() + "-" +
time.getDate() + " " +
time.getHours() + ":" +
time.getMinutes() + ":" +
time.getSeconds();
document.write(timeString);
通過(guò) time來(lái)操作其所引用的Date對(duì)象,可以方便的調(diào)用Date的對(duì)象所包含的一系列g(shù)etXX()方法來(lái)獲取年月日時(shí)分秒等信息。
可以再看一下String
復(fù)制代碼 代碼如下:
var username = new String("hello world");
document.write(username.length);
變量username引用了new出來(lái)的字符串對(duì)象,通過(guò)username訪問(wèn)字符串對(duì)象的length屬性。
2.方法也是對(duì)象
復(fù)制代碼 代碼如下:
function hello() {
alert("hello");
};
var helloRef = hello;
helloRef();
hello是一個(gè)方法,helloRef是一個(gè)引用了hello方法的變量,helloRef和hello一樣都指向了相同的方法對(duì)象。也就意味著helloRef也可以執(zhí)行,helloRef()。同理也可以寫(xiě)出以下代碼。
復(fù)制代碼 代碼如下:
var helloRef = function() {
alert("hello");
};
helloRef();
function(){alert(“hello”)}是一個(gè)匿名方法,當(dāng)然也是對(duì)象,用helloRef變量引用該方法對(duì)象后,可以通過(guò)helloRef來(lái)調(diào)用方法。
3.那么類(lèi)呢?當(dāng)然類(lèi)也是對(duì)象,在javascript中,不像C#或java那樣有class關(guān)鍵字用來(lái)創(chuàng)建類(lèi),而是直接使用方法的關(guān)鍵字來(lái)創(chuàng)建類(lèi)或者叫模擬類(lèi)。
復(fù)制代碼 代碼如下:
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)建了一個(gè)Person類(lèi)型,Person帶有構(gòu)造參數(shù)username和age,通過(guò)創(chuàng)建的Person對(duì)象可以調(diào)用Person所包含的方法Introduce。下面對(duì)代碼做一些修改。
復(fù)制代碼 代碼如下:
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類(lèi),PersonClass和Person都指向了原來(lái)的Person所引用的類(lèi),所以也可以用PersonClass來(lái)創(chuàng)建對(duì)象。
以上的幾個(gè)例子可能不是很恰當(dāng),但也可以一窺javascript中萬(wàn)物皆對(duì)象。
下一節(jié)詳細(xì)的談一談javascript中的對(duì)象。
相關(guān)文章
Javascript面向?qū)ο缶幊蹋ǘ?構(gòu)造函數(shù)的繼承
這個(gè)系列的第一部分,主要介紹了如何"封裝"數(shù)據(jù)和方法,以及如何從原型對(duì)象生成實(shí)例。
2011-08-08
Javascript 面向?qū)ο缶幊?coolshell)
Javascript是一個(gè)類(lèi)C的語(yǔ)言,他的面向?qū)ο蟮臇|西相對(duì)于C++/Java比較奇怪,但是其的確相當(dāng)?shù)膹?qiáng)大,在 Todd 同學(xué)的“對(duì)象的消息模型”一文中我們已經(jīng)可以看到一些端倪了
2012-03-03
JavaScript 對(duì)象的屬性和方法4種不同的類(lèi)型
JavaScript里,對(duì)象的屬性和方法支持4種不同的類(lèi)型,需要的朋友可以參考下。
2010-03-03
javascript 混合的構(gòu)造函數(shù)和原型方式,動(dòng)態(tài)原型方式
JS編程中最常用兩種對(duì)象類(lèi)定義的方式。不管是利用下面2種方式的那一種,都可以達(dá)到相同的效果!
2009-12-12
JavaScript面向?qū)ο笾甈rototypes和繼承
本文翻譯自微軟的牛人Scott Allen Prototypes and Inheritance in JavaScript ,本文對(duì)到底什么是Prototype和為什么通過(guò)Prototype能實(shí)現(xiàn)繼承做了詳細(xì)的分析和闡述,是理解JS OO 的佳作之一
2012-07-07
一實(shí)用的實(shí)現(xiàn)table排序的Javascript類(lèi)庫(kù)
一實(shí)用的實(shí)現(xiàn)table排序的Javascript類(lèi)庫(kù)...
2007-09-09 
