ror体育

文章索引 聚合服务页

工 具 文 档

您现在的位置:  >> 站长教程 >> 网页设计 >> Flash >> 正文
站内文章搜索:

"EAS2边学边练" 之一 类方法 checkType( )

作者:佚名    文章来源:    点击数:6197    更新时间:2008/12/21

注意:如果您了解有关AS2类方法的概念,那么请继续浏览本文;

在进入正题前,先来回顾一下typof():

typeof(expression)

可用性
Flash Player 5。

参数
expression 一个字符串、影片剪辑、按钮、对象或函数。

输出
参数 输出

布尔值 boolean
数字 number
字符串 string
函数 function
影片剪辑 movieclip
按钮 object
文本字段 object
对象 object

以下是使用 typeof( ) 以及Toolkit.builtInOf( ) 测试版的试验结果,只为说明几个问题:

// 布尔值
var test:Boolean = false;
trace("typeof is " + typeof test);
Toolkit.builtInOf(test);
输出:
typeof is boolean
My God! It is not a built-in datatype!
// 布尔对象
var test:Boolean = new Boolean("true");
trace("typeof is " + typeof test);
Toolkit.builtInOf(test);
输出:
typeof is object
This is a instace of Boolean

// 数字值
var test:Number = 5;
trace("typeof is " + typeof test);
Toolkit.builtInOf(test);
输出:
typeof is number
My God! It is not a built-in datatype!
// 数字对象
var test:Number = new Number(5);
trace("typeof is " + typeof test);
Toolkit.builtInOf(test);
输出:
typeof is object
This is a instace of Number

// 字符串值
var test:String = "haha";
trace("typeof is " + typeof test);
Toolkit.builtInOf(test);
输出:
typeof is string
My God! It is not a built-in datatype!
// 字符串对象
var test:String = new String("haha");
trace("typeof is " + typeof test);
Toolkit.builtInOf(test);
输出:
typeof is object
This is a instace of String

//函数
function test() {
// Nothing to do
}
trace("typeof is " + typeof test);
Toolkit.builtInOf(test);
输出:
typeof is function
This is a instace of Function

// 影片剪辑1
// test 是舞台上一矩形影片剪辑的实例名
trace("typeof is " + typeof test);
Toolkit.builtInOf(test);
输出:
typeof is movieclip
This is a instace of MovieClip
// 影片剪辑2
var test:MovieClip = _root.createEmptyMovieClip("tt", 10);
trace("typeof is " + typeof test);
Toolkit.builtInOf(test);
输出:
typeof is movieclip
This is a instace of MovieClip

// 按钮
// test是舞台上一矩形按钮的实例名
trace("typeof is " + typeof test);
Toolkit.builtInOf(test);
输出:
typeof is object
This is a instace of Button

// 文本1
// test 是舞台上一个输入文本框的实例名
trace("typeof is " + typeof test);
Toolkit.builtInOf(test);
输出:
typeof is object
This is a instace of TextField
// 文本2
// test 是舞台上一个动态文本框的实例名
trace("typeof is " + typeof test);
Toolkit.builtInOf(test);
输出:
typeof is object
This is a instace of TextField
// 文本3
// test 是用MovieClip.createTextField() 创建的动态文本的实例名
// 注意:MovieClip.createTextField() 是无返回值的
_root.createTextField("test", 1, 100, 100, 300, 100);
test.border = true;
trace("typeof is " + typeof test);
Toolkit.builtInOf(test);
输出:
typeof is object
This is a instace of TextField

// 对象1--Array对象
var test:Array = [0, 1, 2];
trace("typeof is " + typeof test);
Toolkit.builtInOf(test);
输出:
typeof is object
This is a instace of Array
// 对象2--Date对象
var test:Date = new Date();
trace("typeof is " + typeof test);
Toolkit.builtInOf(test);
输出:
typeof is object
This is a instace of Date
// 对于其他对象的测试,请读者看完本文后自行测试! 这些例子足以说明问题了!

结论:
boolean、number、string(首字母均为小写,若大写就是对象)是语言本身的数据类型,而非对象。
然而各种对象Object是OOP的基础,typeof只能判断出是object(Object类是所有类的超类),
其中函数和影片剪辑是较为特殊的两个,typeof可以判断其类型,同时它们又是对象。
对于其他对象若想判断具体类别就要靠checkType( )了。

源码如下:


//
//:WHY:
// 不断丰富自定义静态方法
//
// :WHAT:
// 200501300304 checkType(varName:Object):Void
// 类型:调试类方法,当然不排除日后用于程序中的可能
// 功能:全面检测变量所属的数据类型(包括自定义类),并将判断结果显示在输出窗口中
// 参数:单一变量
// 返回值:无
//
class Toolkit {
public static function checkType(varName:Object):Void {
var typeResult:String = typeof (varName);
var classResult:String = "Not any Object instance.";
// 逐个类型能检查
switch (typeResult) {
case "boolean" :
break;
case "number" :
break;
case "string" :
break;
case "function" :
// 特殊情况,特殊处理
classResult = "Function";
break;
case "movieclip" :
// 特殊情况,特殊处理
classResult = "MovieClip";
break;
case "object" :
// 进一步判断实例所属的类
classResult = quickCheckClass(varName);
break;
default :
// undefined, null
}
trace(" typeof: " + typeResult + "\nbuiltInOf: " + classResult + "\n");
}
//:WORKING:
// 快速检查给定对象所属类(包括自定义类)的名称
private static function quickCheckClass(instanceName:Object):String {
_global.ASSetPropFlags(_global, null, 6, 1);
for (var className in _global) {
if (instanceName instanceof eval(className)) {
return className;
}
}
}
//:RETIRE:
// 检查给定对象是否属于在 givenClass 所列出的类
private static function checkGivenClass(instanceName:Object):String {
// 70个内建类名称
var givenClass:Array = new Array("XMLUI", "CustomActions", "MMSave", "Cookie", "TextSnapshot", "PrintJob", "MovieClipLoader", "URIError", "TypeError", "SyntaxError", "ReferenceError", "EvalError", "System", "Accessibility", "Video", "Stage", "TextFormat", "TextField", "Button", "Key", "Mouse", "Selection", "LoadVars", "XML", "XMLNode", "Sound", "Math", "Array", "String", "Date", "Boolean", "Number", "o", "profile", "clearInterval", "setInterval", "isFinite", "isNaN", "updateAfterEvent", "trace", "parseFloat", "parseInt", "unescape", "escape", "ASSetNative", "ASSetPropFlags", "AsSetupError", "Error", "ContextMenu", "ContextMenuItem", "LocalConnection", "SharedObject", "Microphone", "Camera", "NetStream", "NetConnection", "Color", "AsBroadcaster", "XMLSocket", "MovieClip", "Infinity", "NaN", "Function", "FWEndCommand", "MMEndCommand", "FWjavascript", "MMExecute", "ASconstructor", "ASnative", "Object");
var numLoop:Number = givenClass.length;
for (var i = 0; i < numLoop; i++) {
if (instanceName instanceof eval(givenClass[i])) {
return givenClass[i];
}
}
}
}

前面的分析已经足够了,这个方法的逻辑结构也很简单(如果你学过一些编程知识的话),但俺保证要比typeof( ) 好用多了!

源文件下载(已修订):
点击浏览该文件

会用的自己搞定就行了!
不会用的,下载后解压缩,将Toolkit.as放入以下目录:
Macromedia\Flash MX 2004\zh_cn\First Run\Classes
或者 Macromedia\Flash MX 2004\en\First Run\Classes
然后重启Flash MX 2004或Flash MX Professional 2004即可测试、使用。 以下是正式版的测试用例及其结果

// test use-case
var test0:Boolean = false;
var test1:Boolean = new Boolean("true");
var test2:Number = 5;
var test3:Number = new Number(5);
var test4:String = "haha";
var test5:String = new String("haha");
function test6() {
//Nothing to do
}
var test7:MovieClip = _root.createEmptyMovieClip("tt", 10);
_root.createTextField("test8", 11, 100, 100, 300, 100);
test8.border = true;
var test9:Array = [0, 1, 2];
var test10:Date = new Date();
//自定义类
var test11:Box = new Box();
// output test result
for (var i = 0; i <= 11; i++) {
trace("Test" + i);
Toolkit.checkType(this["test" + i]);
}

输出:

Test0
typeof: boolean
builtInOf: Not any Object instance.
Test1
typeof: object
builtInOf: Boolean
Test2
typeof: number
builtInOf: Not any Object instance.
Test3
typeof: object
builtInOf: Number
Test4
typeof: string
builtInOf: Not any Object instance.
Test5
typeof: object
builtInOf: String
Test6
typeof: function
builtInOf: Function
Test7
typeof: movieclip
builtInOf: MovieClip
Test8
typeof: object
builtInOf: TextField
Test9
typeof: object
builtInOf: Array
Test10
typeof: object
builtInOf: Date
Test11
typeof: object
builtInOf: Box

文章录入:文章录入:zhangmao    责任编辑:zm 
  • 上一篇文章:
  • 下一篇文章:
  • ror体育相关的文章:
    没有ror体育相关的文章