From: Asen Bozhilov on
Thomas 'PointedEars' Lahn wrote:

> /**
>  * Returns <code>true</code> if the argument is a {@link Map}
>  *
>  * @param o : Object
>  * @return boolean
>  */
> Map.isInstance = function (o) {
>   return !!o && o.constructor === this;
>
> };

But this method is useless in ECMA-262-3 environments. For backward
compatible can be good, but in inheritance pattern is not very well,
because doesn't analyse prototype chain.

function Map2(){}
Map2.prototype = new Map();
Map2.prototype.constructor = Map2;

print(Map.isInstance(new Map2())); //false

What is a benefit from that method at all? It is try to make
generalization, which isn't so precisely. I don't see reasons to test
any objects in this way in implementations, which aren't support
`instanceof` operator and `Object.prototype.isPrototypeOf (V)` method.
If i need something like this, I'll do test over interface of object.
For example if i need to call `getKey' method:

if (typeof o.getKey == 'function') {
o.getKey(key);
}

If i expect somewhere in my code `Map' instance, I'll write
documentation about this case. And of course, I'll don't do it test
above.

Regards ;~)


From: kangax on
On 3/18/10 8:49 AM, Thomas 'PointedEars' Lahn wrote:
> JJ wrote:
>
>> // Action Constants
>> function Action() {}
>> // Action types, Action names
>> Action.Type = { ADD: 0, UPDATE: 1, CLOSE: 2 };
>> Action.Name = [ "add.web", "save.web", "close" ];
>
> Note that those are not real constants, but currently there is no better
> interoperable way.
>
>> [...]
>> if( !sStatus )
>> iActionType = Action.Type.ADD;
>> else
>> iActionType = Action.Type.UPDATE;
>
> Can (and IMHO should) be written as follows:
>
> var iActionType = (sStatus) ? Action.Type.UPDATE : Action.Type.ADD;

Or even:

var iActionType = Action.Type[sStatus ? 'UPDATE' : 'ADD'];

[...]

--
kangax
From: Andrea Giammarchi on
Agreed, also instanceof simply checks the prototype and not the
constructor

If we want rely on constructors and their prototype, why create such
method rather than do this check?

if (o instanceof Map) {

}

it's even shorter than

if (Map.isInstance(o)) {

}

then, as already said

var MProto = Map.prototype;
var m = new Map;

// bye bye
Map.prototype = {};

m instanceof Map; // false
m.constructor === Map; // false

MProto.isPrototypeOf(m); // true

accordingly, believing that isInstance won't be changed, I would
rather go for something like:

Map.isInstance = (function (__proto__) {
return function (o) {
return __proto__.isPrototypeOf(o);
};
}(Map.prototype));

Regards



On Mar 18, 10:10 pm, Asen Bozhilov <asen.bozhi...(a)gmail.com> wrote:
> Thomas 'PointedEars' Lahn wrote:
> > /**
> >  * Returns <code>true</code> if the argument is a {@link Map}
> >  *
> >  * @param o : Object
> >  * @return boolean
> >  */
> > Map.isInstance = function (o) {
> >   return !!o && o.constructor === this;
>
> > };
>
> But this method is useless in ECMA-262-3 environments. For backward
> compatible can be good, but in inheritance pattern is not very well,
> because doesn't analyse prototype chain.
>
> function Map2(){}
> Map2.prototype = new Map();
> Map2.prototype.constructor = Map2;
>
> print(Map.isInstance(new Map2())); //false
>
> What is a benefit from that method at all? It is try to make
> generalization, which isn't so precisely. I don't see reasons to test
> any objects in this way in implementations, which aren't support
> `instanceof` operator and `Object.prototype.isPrototypeOf (V)` method.
> If i need something like this, I'll do test over interface of object.
> For example if i need to call `getKey' method:
>
> if (typeof o.getKey == 'function') {
>   o.getKey(key);
>
> }
>
> If i expect somewhere in my code `Map' instance, I'll write
> documentation about this case. And of course, I'll don't do it test
> above.
>
> Regards ;~)

From: Thomas 'PointedEars' Lahn on
Asen Bozhilov wrote:

> Thomas 'PointedEars' Lahn wrote:
>> /**
>> * Returns <code>true</code> if the argument is a {@link Map}
>> *
>> * @param o : Object
>> * @return boolean
>> */
>> Map.isInstance = function (o) {
>> return !!o && o.constructor === this;
>>
>> };
>
> But this method is useless in ECMA-262-3 environments. For backward
> compatible can be good, but in inheritance pattern is not very well,
> because doesn't analyse prototype chain.

It does not have to.

> function Map2(){}
> Map2.prototype = new Map();

This inheritance pattern is a problem to begin with. Often discussed,
often disapproved of.

> Map2.prototype.constructor = Map2;
>
> print(Map.isInstance(new Map2())); //false

There is no guard against stupidity.

> What is a benefit from that method at all?

That a Map instance can be recognized for what it is. This is important
because there are some methods that are only available to Map instances.
RTSL.

> if (typeof o.getKey == 'function') {
> o.getKey(key);
> }

Doesn't apply here.


PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
From: Thomas 'PointedEars' Lahn on
Andrea Giammarchi wrote:

> Agreed, also instanceof simply checks the prototype and not the
> constructor

The intention here was to avoid just that.

> If we want rely on constructors and their prototype, why create such
> method rather than do this check?
>
> if (o instanceof Map) {
>
> }
>
> it's even shorter than
>
> if (Map.isInstance(o)) {
>
> }

And it is less compatible.

> [non sequitur]
>
> [top post]

Learn to quote.


PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
First  |  Prev  |  Next  |  Last
Pages: 1 2 3 4 5 6 7 8 9 10 11 12
Prev: how to get url of script
Next: question of logic?