How To Discriminate Between JavaScript Object and ActionScript Object In AIR

chart-accept-128x128JavaScript and ActionScript share a nearly identical syntax, object model, and even many object classes because both of them are based on the ECMA-262 (ECMAScript) standard. ActionScript implements the arguments object, which supports the callee and length properties, just like JavaScript. ActionScript also implements the Array, Boolean, Date, Math, Number, Object, and String classes familiar to JavaScript programmers.Adobe AIR allows ActionScript and JavaScript base object can be convert each other.But sometimes we still need to know which is javascript objct and which is actionscript object.The following code shows how to detect Javascript objct and ActionScript.

Detect ActionScript Object in AIR ActionScript runtime:

import flash.html.*;
import flash.utils.*;
import flash.system.*; 

var htmlLdr:HTMLLoader = new HTMLLoader();
var jsRuntime:Object = htmlLdr.window; 

var asObj:Object = new Object();
var asAry:* = new Array();
var asDate:* = new Date();
var asSprite:* = new flash.display.Sprite();
var asXML:* = new XML();
var asXMLList:* = new XMLList();
var asRegExp:* = new RegExp();
var asNS:* = new Namespace();
var asFun:* = function():void{};
var asNo:* = new Number();
var asStr:* = new String();
var asBol:* = new Boolean(); 

var jsObj:Object = new jsRuntime.Object();
var jsAry:Object = new jsRuntime.Array();
var jsDate:Object = new jsRuntime.Date();
var jsFun:Object = new jsRuntime.Function("", "");
var jsNo:Object = new jsRuntime.Number();
var jsStr:Object = new jsRuntime.String();
var jsBol:Object = new jsRuntime.Boolean(); 

function isASObject(o:*):Boolean{
//return o.constructor is Class;
return getQualifiedClassName(o).indexOf("flash.html::__HTMLScript") < 0;

trace(isASObject(asObj)); // true
trace(isASObject(asAry)); // true
trace(isASObject(asDate)); // true
trace(isASObject(asSprite)); // true
trace(isASObject(asXML)); // true
trace(isASObject(asXMLList)); // true
trace(isASObject(asRegExp)); // true
trace(isASObject(asNS)); // true
trace(isASObject(asFun)); // true
trace(isASObject(asNo)); // true
trace(isASObject(asStr)); // true
trace(isASObject(asBol)); // true 

trace(isASObject(jsObj)); // false
trace(isASObject(jsAry)); // false
trace(isASObject(jsDate)); // false
trace(isASObject(jsFun)); // false
trace(isASObject(jsNo)); // true
trace(isASObject(jsStr)); // true
trace(isASObject(jsBol)); // true

Detect JavaScript Object in AIR JavaScript runtime:

var asObj = new runtime.Object();
var asAry = new runtime.Array();
var asDate = new runtime.Date();
var asSprite = new runtime.flash.display.Sprite();
var asXML = new runtime.XML();
var asXMLList = new runtime.XMLList();
var asRegExp = new runtime.RegExp();
var asFun = runtime.trace;
var asNo = new runtime.Number();
var asStr = new runtime.String();
var asBol = new runtime.Boolean(); 

var jsObj = new Object();
var jsAry = new Array();
var jsDate = new Date();
var jsFun = function(){};
var jsNo = 10;
var jsStr = "str";
var jsBol = true; 

function isJSObject(o) {
return o.constructor instanceof Function;

runtime.trace(isJSObject(asObj)); // false
runtime.trace(isJSObject(asAry)); // false
runtime.trace(isJSObject(asDate)); // false
runtime.trace(isJSObject(asSprite)); // false
runtime.trace(isJSObject(asXML)); // false
runtime.trace(isJSObject(asXMLList)); // false
runtime.trace(isJSObject(asRegExp)); // false
runtime.trace(isJSObject(asFun)); // false
runtime.trace(isJSObject(asStr)); // true
runtime.trace(isJSObject(asNo)); // true
runtime.trace(isJSObject(asBol)); // true 

runtime.trace(isJSObject(jsObj)); // true
runtime.trace(isJSObject(jsAry)); // true
runtime.trace(isJSObject(jsDate)); // true
runtime.trace(isJSObject(jsFun)); // true
runtime.trace(isJSObject(jsStr)); // true
runtime.trace(isJSObject(jsNo)); // true
runtime.trace(isJSObject(jsBol)); // true

※Sample source code from ticore(Traditional Chinese)

Share and Enjoy:
  • Digg
  • Facebook
  • Google Bookmarks
  • DZone
  • Reddit
  • Technorati
  • StumbleUpon
  • Twitter
RSS Enjoy this Post? Subscribe to

RSS Feed   RSS Feed     Email Feed  Email Feed Follow us Follow us
You can leave a response, or trackback from your own site.

One Response to “How To Discriminate Between JavaScript Object and ActionScript Object In AIR”

  1. [...] How To Discriminate Between JavaScript Object and ActionScript Object In AIR (0) [...]

Leave a Reply