PDA

View Full Version : instanceof


kentyler
05-20-2003, 06:32 AM
looks like instanceof is not implemented, which is a bummer unless there is some other way of determining the class of an object


<canvas debug="true">
<script>
<![CDATA[
x= new Date();
debug.write( typeof(x) );
debug.write( x instanceof Date );

]]>
</script>
</canvas>

antun
05-20-2003, 08:43 AM
That's right, instanceof isn't yet implemented either in the Flash runtime or by Laszlo.

I don't believe there is a way to find out the class of an object. I believe typeof will only say whether it's an object, right?

-Antun

kentyler
05-20-2003, 09:45 AM
it turns out, you can add a substitute method ".isa()", even to built in objects like Date or Array, as well as to custom classes, like Struct

Here is a sample of how it can be called. The for cf_date, cf_number and cf_object is on laszlouser.org


<canvas debug="true">
<include href="cf_number.lzx"/>
<include href="cf_date.lzx"/>
<include href="cf_object.lzx"/>
<script>
<![CDATA[


function isObject(){ return 'object' }

Object.prototype.isa = isObject;

function isDate(){ return 'date' }

Date.prototype.isa = isDate;

var x = new Date();
var y = new Object();

debug.write( x.isa() );
debug.write( y.isa() );

function isArray() { return 'array' }

Array.prototype.isa = isArray;
var z = new Array();
debug.write( z.isa() );

var w = new Struct();
debug.write( w.isa() );



]]>
</script>
</canvas>

antun
05-20-2003, 11:33 AM
Apparently instanceof should work for LZX objects (such as view, node and so forth), but not the core JavaScript objects.

-Antun

bstow01
04-27-2007, 05:02 PM
try myObj.toString()... this will get the name of the class

jamesej
05-04-2007, 06:22 AM
Yup I've tried instanceof with Laszlo objects it also seems to work hierarchically so

var text = new LzText(this);
var isaview = (text instanceof LzView);

should set isaview to true.