用?new?Object()?來創(chuàng)建對象
在javascript里有幾種創(chuàng)建對象的方法,在不同的場合可用不同的方法.最簡單的就是用?new?操作符,例如:
<script> <BR><!-- <br><br>person = new Object() <BR>person.name = "Tim Scarfe" <BR>person.height = "6Ft" <br><br>person.run = function() { <BR>this.state = "running" <BR>this.speed = "4ms^-1" <BR>} <br><br>//--> <BR></script>?
我們在這個例子里定義了person這個對象,然后加入了它的屬性和方法.在這個例子里,自定義的方法里有兩個屬性.
用文字記號Literal?Notation創(chuàng)建對象
用文字記號也可以創(chuàng)建對象,但要javascript?1.2以上版本.它的創(chuàng)建形式是多樣的.
<script> <BR><!-- <br><br>// Object Literals <br><br>timObject = { <BR>property1 : "Hello", <BR>property2 : "MmmMMm", <BR>property3 : ["mmm", 2, 3, 6, "kkk"], <BR>method1 : function(){alert("Method had been called" + this.property1)} <BR>}; <br><br>timObject.method1(); <BR>alert(timObject.property3[2]) // will yield 3 <br><br>var circle = { x : 0, y : 0, radius: 2 } // another example <br><br>// nesting is no problem. <BR>var rectangle = { <BR>upperLeft : { x : 2, y : 2 }, <BR>lowerRight : { x : 4, y : 4} <BR>} <br><br>alert(rectangle.upperLeft.x) // will yield 2 <br><br>//--> <BR></script>?
文字記號可是是數(shù)組,也可以是任意的javascript表達(dá)式或值.
用?new?操作符或文字記號創(chuàng)建一個自定義對象都是簡單的,也是符合邏輯的.但它最大的缺點就是結(jié)果不可復(fù)用.也不能很容易的用不同的版本初始化創(chuàng)建對象.例如上面?的第一個例子,如果?person?的?name?不是?"Tim?Scarfe",那樣我們不得不重新定義整個對象,僅僅為了適應(yīng)它的一點點改變.
對象的構(gòu)造和原型
????在OOP的世界里,用先前的方法定義對象在許多場合都有限制.我們需要一種創(chuàng)建對象的方法,類型可以被多次使用而不用重新定義.對象在實例化時每次都可以按需分配不同的值.實現(xiàn)這個目標(biāo)的標(biāo)準(zhǔn)方法是用對象構(gòu)造器函數(shù).
???一個對象構(gòu)造器只不過是個有規(guī)則的javascript函數(shù),它就象一個容器(定義參數(shù),調(diào)用其他函數(shù)等等).它們兩者所不同的是構(gòu)造器函數(shù)是由?new?操作符調(diào)用的.(你將在下面的例子中看到).基于函數(shù)語法形式的對象定義,我們可以使它工作得最好.
讓我們用現(xiàn)實世界中的貓來舉個例子.貓的?name?和?color?是貓的屬性.meeyow(貓叫)是它的一個方法.重要的是任何不同的貓都可能有不同的?name?和?meeyow?的叫聲.為了建立適應(yīng)這些特征的對象類,我們將使用對象構(gòu)造器.?
<script> <BR><!-- <br><br>function cat(name) { <BR>this.name = name; <BR>this.talk = function() { <BR>alert( this.name + " say meeow!" ) <BR>} <BR>} <br><br>cat1 = new cat("felix") <BR>cat1.talk() //alerts "felix says meeow!" <br><br>cat2 = new cat("ginger") <BR>cat2.talk() //alerts "ginger says meeow!" <br><br>//--> <BR></script>
在這里,函數(shù)?cat()?是一個對象構(gòu)造器,它的屬性和方法在函數(shù)體里用this來定義,用對象構(gòu)造器定義的對象用?new?來實例化.主意我們?nèi)绾畏浅H菀椎亩x多個cat?的實例.每一個都有自己的名字,這就是對象構(gòu)造器帶給我們的靈活性.
構(gòu)造器建立了對象的藍(lán)圖.并不是對象本身.
在原型里增加方法.
在上面我們看到的例子里,對象的方法是在構(gòu)造器里定義好的了.另外一種實現(xiàn)的途徑是通過原型(prototype).xxx
原型是javascript繼承的一種形式.我們可以為對象定義好后,再創(chuàng)造一個方法.原來所有對象的實例都將共享.
讓我們來擴展最初的?cat?對象.增加一個改名的方法.用?prototype?的方式.?
<script> <BR><!-- <br><br>cat.prototype.changeName = function(name) { <BR>this.name = name; <BR>} <br><br>firstCat = new cat("pursur") <BR>firstCat.changeName("Bill") <BR>firstCat.talk() //alerts "Bill says meeow!" <br><br>//--> <BR></script>
就象你所看到的.我們僅只用了?關(guān)鍵字?prototype?實現(xiàn)了在對象定義后馬上增加了changeName方法.這個方法被所有的實例共享.
用原型(prototype)?重載?javascript?對象
原型?在自定義對象和有選擇性的重載對象上都可以工作.比如?Date()?或?String?這可能是無止境的.
子類和超類
在JAVA?和C++里,有關(guān)于類層次的外在概念.每一個類能有一個角色.
In?Java?and?C++,?there?is?an?explicit?concept?of?the?class?hierarchy.?i.e.?Every?class?can?have?a?super?class?from?which?it?inherits?properties?and?methods.?Any?class?can?be?extended,?or?sub-classed?so?the?resulting?subclass?can?inherit?its?parent's?behavior.?As?we?have?seen,?javascript?supports?prototype?inheritance?instead?of?class?based.?It's?possible?for?inheritance?to?happen?other?ways,?however.
The?following?is?an?example?of?inheritance?through?functions.
下面一個例子演示了如何繼承通過function?.?
<script> <BR><!-- <br><br>// thanks to webreference <br><br>function superClass() { <BR>this.supertest = superTest; //attach method superTest <BR>} <br><br>function subClass() { <BR>this.inheritFrom = superClass; <BR>this.inheritFrom(); <BR>this.subtest = subTest; //attach method subTest <BR>} <br><br>function superTest() { <BR>return "superTest"; <BR>} <br><br>function subTest() { <BR>return "subTest"; <BR>} <br><br><BR>var newClass = new subClass(); <br><br>alert(newClass.subtest()); // yields "subTest" <BR>alert(newClass.supertest()); // yields "superTest" <br><br>//--> <BR></script>
基于繼承的原型是遙遠(yuǎn)的.為?javascript?應(yīng)用程序在許多場合.
(基于原型的繼承在許多javascript的應(yīng)用場合是非常有用的.)
對象作為聯(lián)合數(shù)組
正如你所知,?(.)操作符能夠用來存儲.[]?操作符用來操作數(shù)組.
<script> <BR><!-- <br><br>// These are the same <BR>object.property <BR>object["property"] <br><br>//--> <BR></script>
<script> <BR><!-- <BR>function Circle (xPoint, yPoint, radius) { <BR>this.x = xPoint; <BR>this.y = yPoint; <BR>this.r = radius; <BR>} <br><br>var aCircle = new Circle(5, 11, 99); <BR>alert(aCircle.x); <BR>alert(aCircle["x"]); <BR>//--> <BR></script>
How?do?I?loop?through?properties?in?an?object?
You?need?to?use?a?for/in?loop.
我們可以通過for?in循環(huán)來遍歷對象的屬性。
<script> <BR><!-- <br><br>var testObj = {<BR>prop1 : "hello", <BR>prop2 : "hello2", <BR>prop3 : new Array("hello",1,2) <BR>} <BR>for(x in testObj) alert( x + "-" + testObj[ x ] ) <BR>//--> <BR></script>
<script> <BR><!-- <BR>var Circle = { x : 0, y : 1, radius: 2 } // another example <br><br>for(p in Circle) <BR>alert( p + "-" + Circle[ p ] ) <BR>//--> <BR></script>
The?important?thing?to?notice?is?that?in?the?object?syntax?the?property?is?an?identifier,?whereas?in?the?array?syntax,?it's?a?string.?The?obvious?benefits?of?using?an?array?syntax?to?access?an?object?is?because?of?the?literal?data?type,?you?can?easily?concat?strings?and?play?around?with?them?to?access?an?object.?For?this?to?work?with?the?standard?syntax,?an?eval()?would?need?to?be?used.
應(yīng)該值得注意的是對象的屬性只是一個標(biāo)識字符,盡管在一個數(shù)組里是一個字符串,因為是一個literal的數(shù)據(jù)類型,所以有利于使用數(shù)組的方式的操作一個對象。你也可以很容易的存取一個對象在標(biāo)準(zhǔn)的語句中。這個時候eval()函數(shù)可能用得到。
<script> <BR><!-- <br><br>testObj = { <BR>prop1 : "hello", <BR>prop2 : "hello2", <BR>prop3 : new Array("helloa",1,2) <BR>} <br><br>for(x in testObj) alert( x + "-" + testObj[ x ] ) <BR>var prop3 = testObj["prop3"]; <BR>alert(prop3); <BR>//alert(prop[1]); <BR>alert(typeof(prop3)); <BR>alert(eval(prop3)[1]); <BR>alert(typeof(eval(prop3)[1])); <br><br>//--> <BR></script>
網(wǎng)上的東西錯誤的太多了,jb51.net修正后的測試下