注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

神魔破杜梓的叨叨堂

Programming every day!

 
 
 

日志

 
 
 
 

AS3类型检查的新方法  

2008-08-12 15:17:47|  分类: My Tech |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
原文来自frankieloscavio.blogspot.com

Today Ryan and I were going through the Flex 3 Framework classes to see how the messaging and ASyncToken classes are setup and while going through the code we saw something like this:

var Obj:Object = this;

if(Obj.constructor == MyClass){

Alert.show('This is another way of doing a typeof or is operator type checking ');

}

I changed the example a little for this post, but it was pretty close to what you see above. I thought it was an interesting discovery since it was actually used in the framework. It appears to be one the the very scripty parts of ActionScript that still remains in AS3. Hence maybe why it's still a scripting language? Anyway's, it was a discovery and thought I'd share it along.

 

Check below and the link for a more detailed reference if you are curious like I was:

Comment - "Object.constructor is documented, but not recognized by the Flex Builder 2 compiler. Attempting to access the constructor property of a non-dynamic object generates a compile-time error" - _depth_ said on Nov 7, 2007 at 10:17 AM

 

Quote from Livedocs - "A reference to the class object or constructor function for a given object instance. If an object is an instance of a class, the constructor property holds a reference to the class object. If an object is created with a constructor function, the constructor property holds a reference to the constructor function. Do not confuse a constructor function with a constructor method of a class. A constructor function is a Function object used to create objects, and is an alternative to using the class keyword for defining classes.

If you use the class keyword to define a class, the class's prototype object is assigned a property named constructor that holds a reference to the class object. An instance of the class inherits this property from the prototype object. For example, the following code creates a new class, A, and a class instance named myA:

  dynamic class A {}
trace(A.prototype.constructor); // [class A]
trace(A.prototype.constructor == A); // true
var myA:A = new A();
trace(myA.constructor == A); // true

Advanced users may choose to use the function keyword instead of the class keyword to define a Function object that can be used as a template for creating objects. Such a function is called a constructor function because you can use it in conjunction with the new operator to create objects. If you use the function keyword to create a constructor function, its prototype object is assigned a property named constructor that holds a reference to the constructor function. If you then use the constructor function to create an object, the object inherits the constructor property from the constructor function's prototype object. For example, the following code creates a new constructor function, f, and an object named myF:

  function f() {}
trace(f.prototype.constructor); // function Function() {}
trace(f.prototype.constructor == f); // true
var myF = new f();
trace(myF.constructor == f); // true

Note: The constructor property is writable, which means that user code can change its value with an assignment statement. Changing the value of the constructor property is not recommended, but if you write code that depends on the value of the constructor property, you should ensure that the value is not reset. The value can be changed only when the property is accessed through the prototype object (for example, className.prototype.constructor)."


http://livedocs.adobe.com/flex/2/langref/Object.html


  评论这张
 
阅读(361)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017