Javascript 對(duì)象的解釋
更新時(shí)間:2008年11月24日 21:39:31 作者:
ECMAScript沒(méi)有像C++,Smalltalk,或者java中那樣規(guī)矩的類,可是它支持通過(guò)執(zhí)行分配空間的代碼來(lái)創(chuàng)建對(duì)象、并初始化對(duì)象所有或者一部分屬性的構(gòu)造器。
所有的構(gòu)造器都是對(duì)象,而并非所有的對(duì)象都是構(gòu)造器.每個(gè)構(gòu)造器都有一個(gè)用來(lái)實(shí)現(xiàn)原型繼承、共享屬性的Prototype屬性。對(duì)象通過(guò)new 表達(dá)式創(chuàng)建;比如,new String("A String") 創(chuàng)建了一個(gè)String對(duì)象。沒(méi)有通過(guò)new而直接調(diào)用構(gòu)造器將有返回值,返回的類型將取決于構(gòu)造器。例如String("A String")產(chǎn)生一個(gè)原始的類型的字符串而不是一個(gè)對(duì)象。
ECMAScript支持基于原型的繼承。每個(gè)構(gòu)造器都有一個(gè)與之關(guān)聯(lián)的原型,而且通過(guò)此構(gòu)造器創(chuàng)建的對(duì)象都有一個(gè)與構(gòu)造器原型關(guān)聯(lián)的隱式引用(稱為,對(duì)象的原型)。進(jìn)一步說(shuō),一個(gè)原型可能有一個(gè)對(duì)其原型的非空隱式引用……,這被稱為,原型鏈。當(dāng)一個(gè)引用指向?qū)ο蟮膶傩?,這個(gè)引用指向原型鏈中的第一個(gè)對(duì)象的以此為名的屬性。換句話說(shuō),第一次,這個(gè)直接關(guān)聯(lián)的對(duì)象,將為這個(gè)屬性被檢查。如果這個(gè)對(duì)象包含以此為名的屬性,這個(gè)屬性就是引用指向的屬性。如過(guò)這個(gè)對(duì)象不包含以此為名的屬性,那么這個(gè)對(duì)象的原型將會(huì)被繼續(xù)檢查……
原文:
Object
ECMAScript does not contain proper classes such as those in C++, Smalltalk, or Java, but rather,supports constructors which create objects by executing code that allocates storage for the objects and initialises all or part of them by assigning initial values to their properties. All constructors are objects,but not all objects are constructors. Each constructor has a Prototype property that is used to implement prototype-based inheritance and shared properties. Objects are created by using constructors in new expressions; for example, new String("A String") creates a new String object. Invoking a constructor without using new has consequences that depend on the constructor. For example,String("A String") produces a primitive string, not an object.
ECMAScript supports prototype-based inheritance. Every constructor has an associated prototype, and every object created by that constructor has an implicit reference to the prototype (called the object's prototype) associated with its constructor. Furthermore, a prototype may have a non-null implicit reference to its prototype, and so on; this is called the prototype chain. When a reference is made to a property in an object, that reference is to the property of that name in the first object in the prototype chain that contains a property of that name. In other words, first the object mentioned directly is examined for such a property; if that object contains the named property, that is the property to which the reference refers; if that object does not contain the named property, the prototype for that object is examined next; and so on.
ECMAScript支持基于原型的繼承。每個(gè)構(gòu)造器都有一個(gè)與之關(guān)聯(lián)的原型,而且通過(guò)此構(gòu)造器創(chuàng)建的對(duì)象都有一個(gè)與構(gòu)造器原型關(guān)聯(lián)的隱式引用(稱為,對(duì)象的原型)。進(jìn)一步說(shuō),一個(gè)原型可能有一個(gè)對(duì)其原型的非空隱式引用……,這被稱為,原型鏈。當(dāng)一個(gè)引用指向?qū)ο蟮膶傩?,這個(gè)引用指向原型鏈中的第一個(gè)對(duì)象的以此為名的屬性。換句話說(shuō),第一次,這個(gè)直接關(guān)聯(lián)的對(duì)象,將為這個(gè)屬性被檢查。如果這個(gè)對(duì)象包含以此為名的屬性,這個(gè)屬性就是引用指向的屬性。如過(guò)這個(gè)對(duì)象不包含以此為名的屬性,那么這個(gè)對(duì)象的原型將會(huì)被繼續(xù)檢查……
原文:
Object
ECMAScript does not contain proper classes such as those in C++, Smalltalk, or Java, but rather,supports constructors which create objects by executing code that allocates storage for the objects and initialises all or part of them by assigning initial values to their properties. All constructors are objects,but not all objects are constructors. Each constructor has a Prototype property that is used to implement prototype-based inheritance and shared properties. Objects are created by using constructors in new expressions; for example, new String("A String") creates a new String object. Invoking a constructor without using new has consequences that depend on the constructor. For example,String("A String") produces a primitive string, not an object.
ECMAScript supports prototype-based inheritance. Every constructor has an associated prototype, and every object created by that constructor has an implicit reference to the prototype (called the object's prototype) associated with its constructor. Furthermore, a prototype may have a non-null implicit reference to its prototype, and so on; this is called the prototype chain. When a reference is made to a property in an object, that reference is to the property of that name in the first object in the prototype chain that contains a property of that name. In other words, first the object mentioned directly is examined for such a property; if that object contains the named property, that is the property to which the reference refers; if that object does not contain the named property, the prototype for that object is examined next; and so on.
相關(guān)文章
JavaScript 使用簡(jiǎn)略語(yǔ)法創(chuàng)建對(duì)象的代碼
JavaScript 使用簡(jiǎn)略語(yǔ)法創(chuàng)建對(duì)象的代碼 ,需要的朋友可以參考下。2010-01-01
JavaScript為對(duì)象原型prototype添加屬性的兩種方式
為對(duì)象原型prototype添加屬性的的方法, 需要的朋友可以參考下。2010-08-08
JavaScript面象對(duì)象設(shè)計(jì)
學(xué)習(xí)js的高境界就是使用面向?qū)ο蟮姆椒?,?shí)現(xiàn)js的調(diào)用2008-04-04
javascript 設(shè)計(jì)模式之單體模式 面向?qū)ο髮W(xué)習(xí)基礎(chǔ)
單體是在腳本加載時(shí)創(chuàng)建的,能將一系列有關(guān)聯(lián)的變量和方法組織為一個(gè)邏輯單元,邏輯單元里面的內(nèi)容通過(guò)單一的變量進(jìn)行訪問(wèn),也是筆記基礎(chǔ)與常用的面向?qū)ο蟮亩x方法。2010-04-04
javascript 面向?qū)ο缶幊? function是方法(函數(shù))
在進(jìn)行編程時(shí),必免不了要碰到復(fù)雜的功能。初學(xué)者最怕復(fù)雜的功能,因?yàn)椴荒軌蚝芎玫倪M(jìn)行功能邊界劃分,只能一大串if、循環(huán)加case堆疊在一起,結(jié)果出來(lái)的程序自己看著暈,別人看著更暈。2009-09-09
Javascript面向?qū)ο髷U(kuò)展庫(kù)代碼分享
最近一直在用js做項(xiàng)目,遇到了許多需要應(yīng)用面向?qū)ο髞?lái)設(shè)計(jì)的功能,由于js對(duì)OOP的原生支持還不是很完善,所以就寫(xiě)了一個(gè)面向?qū)ο蟮臄U(kuò)展庫(kù)用做底層支持,現(xiàn)在把它單獨(dú)整理出來(lái),完善了一些功能,在這里分享一下2012-03-03
javascript 面向?qū)ο缶幊袒A(chǔ) 多態(tài)
javascript 面向?qū)ο缶幊袒A(chǔ) 多態(tài) 的實(shí)現(xiàn)方法說(shuō)明,大家可以看下下面的代碼。
2009-08-08
JavaScript 工具庫(kù) Cloudgamer JavaScript Library v0.1 發(fā)布
研究了一年多的js,也差不多寫(xiě)一個(gè)自己的js庫(kù)了。
我寫(xiě)這個(gè)不算框架,只是一個(gè)小型的js工具庫(kù),所以我用的名字是Library。
2009-10-10 
