Object對(duì)象的一些的隱藏函數(shù)介紹
更新時(shí)間:2006年08月27日 00:00:00 作者:
原作者:fictiony
出自:藍(lán)色理想
自己寫一套類模型的時(shí)候順便整理出來(lái)的,貼出來(lái)給大家看看,希望能對(duì)大家有所幫助。
屬性:Object.constructor
該屬性被定義在類的prototype中,當(dāng)對(duì)象實(shí)例創(chuàng)建后通過(guò)__proto__鏈可被對(duì)象實(shí)例所調(diào)用,并指向當(dāng)前類的構(gòu)造函數(shù)。以此可判斷某個(gè)對(duì)象直接所屬的類是哪個(gè)(與instanceof不同,instanceof并不局限于對(duì)象直接所屬的類,即使是父類也返回true)。
[示例]
trace(Object.prototype.constructor == Object); //輸出 true
var a = new Object();
trace(a.constructor == Object); //輸出 true
var b = new Array();
trace(b.constructor == Array); //輸出 true
trace(b.constructor == Object); //輸出 false
trace(b instanceof Object); //輸出 true
屬性:Object.__constructor__
該屬性功能和Object.constructor相似,區(qū)別在于它不是定義在類的prototype中的,而是當(dāng)對(duì)象實(shí)例創(chuàng)建時(shí)附加到對(duì)象實(shí)例上的。同時(shí),該屬性也被super關(guān)鍵字作為父類構(gòu)造函數(shù)使用時(shí)所隱含調(diào)用,用于指向父類的構(gòu)造函數(shù),即super(...)等價(jià)于 this.__constructor__.call(this, ...)。
[示例]
trace(Object.prototype.__constructor__ == Object); //輸出 false
var a = new Object();
trace(a.__constructor__ == Object); //輸出 true
var b = new Array();
trace(b.__constructor__ == Array); //輸出 true
trace(b.__constructor__ == Object); //輸出 false
方法:Object.isPrototypeOf(classFunc)
該方法用來(lái)判斷當(dāng)前對(duì)象是否在對(duì)象obj的__proto__鏈中。該方法可用來(lái)判斷一個(gè)類是否另一個(gè)類的父類或子類。
[示例]
trace(Object.prototype.isPrototypeOf(new Object())); //輸出 true
trace(Object.prototype.isPrototypeOf(new Array())); //輸出 true
trace(Array.prototype.isPrototypeOf(new Object())); //輸出 false
trace(Object.prototype.isPrototypeOf(Array.prototype)); //判斷Object是否Array的父類,輸出 true
方法:Object.isPropertyEnumerable(propName)
該方法用來(lái)判斷名為propName的成員是否在當(dāng)前對(duì)象中存在并且可被列舉(使用for..in),換句話說(shuō)也就是是否可見(使用ASSetPropFlags全局函數(shù)可設(shè)置對(duì)象屬性是否可見)。
[示例]
var a = {x:1, y:2};
ASSetPropFlags(a, ["y"], 1); //設(shè)y為不可見
trace(a.y); //仍可輸出 2
for (var i in a) trace(i); //僅輸出 x
trace(a.isPropertyEnumerable("x")); //輸出 true
trace(a.isPropertyEnumerable("y")); //輸出 false
方法:Object.hasOwnProperty(propName)
該方法用來(lái)判斷名為propName的成員是否是當(dāng)前對(duì)象自己的成員,而非通過(guò)__proto__鏈從類的prototype中引用過(guò)來(lái)的。
[示例]
function test () {}
test.prototype.x = 1;
var a = new test();
a.y = 2;
trace(a.x); //輸出 1
trace(a.hasOwnProperty("x")); //輸出 false
trace(a.y); //輸出 2
trace(a.hasOwnProperty("y")); //輸出 true
方法:Object.toString()
該方法可定義一個(gè)對(duì)象在轉(zhuǎn)換成字符串類型時(shí)所產(chǎn)生的字符串結(jié)果,一般定義在類的prototype中。
[示例]
function point (x, y) {
this.x = x;
this.y = y;
}
point.prototype.toString = function () {
return "[x:" + this.x + ", y:" + this.y + "]";
};
var pos = new point(10, 20);
trace("position is " + pos); //輸出 position is [x:10, y:20]
出自:藍(lán)色理想
自己寫一套類模型的時(shí)候順便整理出來(lái)的,貼出來(lái)給大家看看,希望能對(duì)大家有所幫助。
屬性:Object.constructor
該屬性被定義在類的prototype中,當(dāng)對(duì)象實(shí)例創(chuàng)建后通過(guò)__proto__鏈可被對(duì)象實(shí)例所調(diào)用,并指向當(dāng)前類的構(gòu)造函數(shù)。以此可判斷某個(gè)對(duì)象直接所屬的類是哪個(gè)(與instanceof不同,instanceof并不局限于對(duì)象直接所屬的類,即使是父類也返回true)。
[示例]
trace(Object.prototype.constructor == Object); //輸出 true
var a = new Object();
trace(a.constructor == Object); //輸出 true
var b = new Array();
trace(b.constructor == Array); //輸出 true
trace(b.constructor == Object); //輸出 false
trace(b instanceof Object); //輸出 true
屬性:Object.__constructor__
該屬性功能和Object.constructor相似,區(qū)別在于它不是定義在類的prototype中的,而是當(dāng)對(duì)象實(shí)例創(chuàng)建時(shí)附加到對(duì)象實(shí)例上的。同時(shí),該屬性也被super關(guān)鍵字作為父類構(gòu)造函數(shù)使用時(shí)所隱含調(diào)用,用于指向父類的構(gòu)造函數(shù),即super(...)等價(jià)于 this.__constructor__.call(this, ...)。
[示例]
trace(Object.prototype.__constructor__ == Object); //輸出 false
var a = new Object();
trace(a.__constructor__ == Object); //輸出 true
var b = new Array();
trace(b.__constructor__ == Array); //輸出 true
trace(b.__constructor__ == Object); //輸出 false
方法:Object.isPrototypeOf(classFunc)
該方法用來(lái)判斷當(dāng)前對(duì)象是否在對(duì)象obj的__proto__鏈中。該方法可用來(lái)判斷一個(gè)類是否另一個(gè)類的父類或子類。
[示例]
trace(Object.prototype.isPrototypeOf(new Object())); //輸出 true
trace(Object.prototype.isPrototypeOf(new Array())); //輸出 true
trace(Array.prototype.isPrototypeOf(new Object())); //輸出 false
trace(Object.prototype.isPrototypeOf(Array.prototype)); //判斷Object是否Array的父類,輸出 true
方法:Object.isPropertyEnumerable(propName)
該方法用來(lái)判斷名為propName的成員是否在當(dāng)前對(duì)象中存在并且可被列舉(使用for..in),換句話說(shuō)也就是是否可見(使用ASSetPropFlags全局函數(shù)可設(shè)置對(duì)象屬性是否可見)。
[示例]
var a = {x:1, y:2};
ASSetPropFlags(a, ["y"], 1); //設(shè)y為不可見
trace(a.y); //仍可輸出 2
for (var i in a) trace(i); //僅輸出 x
trace(a.isPropertyEnumerable("x")); //輸出 true
trace(a.isPropertyEnumerable("y")); //輸出 false
方法:Object.hasOwnProperty(propName)
該方法用來(lái)判斷名為propName的成員是否是當(dāng)前對(duì)象自己的成員,而非通過(guò)__proto__鏈從類的prototype中引用過(guò)來(lái)的。
[示例]
function test () {}
test.prototype.x = 1;
var a = new test();
a.y = 2;
trace(a.x); //輸出 1
trace(a.hasOwnProperty("x")); //輸出 false
trace(a.y); //輸出 2
trace(a.hasOwnProperty("y")); //輸出 true
方法:Object.toString()
該方法可定義一個(gè)對(duì)象在轉(zhuǎn)換成字符串類型時(shí)所產(chǎn)生的字符串結(jié)果,一般定義在類的prototype中。
[示例]
function point (x, y) {
this.x = x;
this.y = y;
}
point.prototype.toString = function () {
return "[x:" + this.x + ", y:" + this.y + "]";
};
var pos = new point(10, 20);
trace("position is " + pos); //輸出 position is [x:10, y:20]
相關(guān)文章
Asp類 的數(shù)據(jù)庫(kù)領(lǐng)域
Asp類 的數(shù)據(jù)庫(kù)領(lǐng)域...2006-10-10一個(gè)簡(jiǎn)單的asp數(shù)據(jù)庫(kù)操作類
一個(gè)簡(jiǎn)單的asp數(shù)據(jù)庫(kù)操作類...2006-08-08