You're right. So long as we make sure the constructor is properly
set, per my message that just crossed with yours.
I had suggested
o.constructor == c
but that is technically wrong. I can make an object with c, then
change c's prototype, and I have effectively created a new class. So
you are correct that you actually have to compare prototypes.
[N.B. We _must_ set the constructor field in our constructors, or
this won't work. The ECMA spec only calls for the constructor
property to be set on the implicitly created prototype. If you
implement inheritance by re-assigning the prototype, you must adjust
the constructor field. In my class proposal, I set the constructor
field on each instance, so that the constructor field in the
prototype does not have to be changed (it represents the inheritance
chain).
I have not looked at the current kernel implementation to see whether
it does this.]
On 2006-04-18, at 11:53 EDT, Adam Wolff wrote:
> what about
>
> o.constructor.prototype == c.prototype
>
> On Apr 18, 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