Oh, if we implement my class proposal completely, which requires  
_correctly_ setting the constructor field on all objects, then you  
can test for an object being a direct instance of a class by:

o.constructor === c

[The spec calls for the constructor property of every object to be  
defined, but some implementations get it wrong (they let it be  
inherited from the prototype, which in the case where you replace the  
default prototype with an instance of another class to get  
inheritance will be
wrong).  In my proposal, I have the class factory say:

this.constructor = arguments.callee

to override the (possibly bogus) inherited constructor property.   
Since this property is defined to exist in all Javascript objects it  
seems fine to at least make it useful.]

On 2006-04-18, at 11:49 EDT, P T Withington wrote:

> o.__proto__ == c.prototype
>
> is usually equivalent to:
>
> o instanceof c
>
> except in the case where you are trying to see if o is a _direct_  
> instance of c.  But that does not seem to be the case here.
>
> I cannot find a way in ECMA to ask if something is a direct  
> instance of a class.  both instanceof and isPrototypeOf test for  
> direct and indirect inheritance.
>
> On 2006-04-18, at 11:27 EDT, Henry Minsky wrote:
>
>> This code in LzDataElement tries to classify an object arg, o, by  
>> looking at
>> it's __proto__ field.
>> Do we have  protocol for finding object's class type that doesn't  
>> rely on
>> __proto__ (which won't work
>> in IE 6 javascript, I believe)
>>
>>
>>
>> LzDataElement.__LZv2E = function ( o ) {
>>
>>     var type = typeof( o );
>>     type.toLowerCase();
>>
>>     var c = [];
>>     if (type == "object") {
>>         var proto = o.__proto__;
>>         if ( proto == LzDataElement.prototype ||
>>              proto == LzDataNode.prototype ) {
>>             c[0] = o;
>>         } else if (proto == Date.prototype) {
>>
>>             type = "date";
>>             // FIXME: [2004-04-10 pkang] what should we do with  
>> dates?
>>
>>         } else if (proto == Array.prototype) {
>>             type = "array";
>>             var tag = (o.__LZtag != null ? o.__LZtag : 'item');
>>             for (var i=0; i < o.length; i++) {
>>                 var tmpC = this.__LZv2E( o[i] );
>>                 c[i] = new LzDataElement(tag, null, tmpC );
>>             }
>>         } else {
>>             type = "struct";
>>             var i = 0;
>>             for (var k in o) {
>>                 // skip any properties that start with __LZ
>>                 if (k.indexOf('__LZ') == 0) continue;
>>                 c[i++] = new LzDataElement(k, null, this.__LZv2E(o 
>> [k]));
>>             }
>>         }
>>     } else if (o != null) {
>>         c[0] = new LzDataText( o );
>>     }
>>
>>     if (c.length == 0) c = null;
>>
>>     return c;
>> }
>>
>> --
>> Henry Minsky
>> Software Architect
>> [EMAIL PROTECTED]
>

_______________________________________________
Laszlo-dev mailing list
[email protected]
http://www.openlaszlo.org/mailman/listinfo/laszlo-dev

Reply via email to