Object对象的一些的隐藏函数介绍

王朝other·作者佚名  2008-05-19
窄屏简体版  字體: |||超大  

属性:Object.constructor

该属性被定义在类的prototype中,当对象实例创建后通过__proto__链可被对象实例所调用,并指向当前类的构造函数。以此可判断某个对象直接所属的类是哪个(与instanceof不同,instanceof并不局限于对象直接所属的类,即使是父类也返回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

代码拷贝框

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

[Ctrl+A 全部选择 然后拷贝]

属性:Object.__constructor__

该属性功能和Object.constructor相似,区别在于它不是定义在类的prototype中的,而是当对象实例创建时附加到对象实例上的。同时,该属性也被super关键字作为父类构造函数使用时所隐含调用,用于指向父类的构造函数,即super(...)等价于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

代码拷贝框

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

[Ctrl+A 全部选择 然后拷贝]

方法:Object.isPrototypeOf(classFunc)

该方法用来判断当前对象是否在对象obj的__proto__链中。该方法可用来判断一个类是否另一个类的父类或子类。

[示例]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

代码拷贝框

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

[Ctrl+A 全部选择 然后拷贝]

方法:Object.isPropertyEnumerable(propName)

该方法用来判断名为propName的成员是否在当前对象中存在并且可被列举(使用for..in),换句话说也就是是否可见(使用ASSetPropFlags全局函数可设置对象属性是否可见)。

[示例]var a = {x:1, y:2};

ASSetPropFlags(a, ["y"], 1); //设y为不可见

trace(a.y); //仍可输出 2

for (var i in a) trace(i); //仅输出 x

trace(a.isPropertyEnumerable("x")); //输出 true

trace(a.isPropertyEnumerable("y")); //输出 false

代码拷贝框

var a = {x:1, y:2};

ASSetPropFlags(a, ["y"], 1); //设y为不可见

trace(a.y); //仍可输出 2

for (var i in a) trace(i); //仅输出 x

trace(a.isPropertyEnumerable("x")); //输出 true

trace(a.isPropertyEnumerable("y")); //输出 false

[Ctrl+A 全部选择 然后拷贝]

方法:Object.hasOwnProperty(propName)

该方法用来判断名为propName的成员是否是当前对象自己的成员,而非通过__proto__链从类的prototype中引用过来的。

[示例]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

代码拷贝框

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

[Ctrl+A 全部选择 然后拷贝]

方法:Object.toString()

该方法可定义一个对象在转换成字符串类型时所产生的字符串结果,一般定义在类的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]

代码拷贝框

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]

[Ctrl+A 全部选择 然后拷贝]

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航