By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,143 Members | 855 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,143 IT Pros & Developers. It's quick & easy.

Intercept instanceof

P: n/a
Hi,

Is it possible to intercept a call to "instanceof" and return something
different? I wanted to see if it was possible to implement something
like multiple inheritance, especially to use instanceof where a
component extends an object and implements some listener interface too
for a MVC architecture.

Jan 21 '08 #1
Share this Question
Share on Google+
13 Replies


P: n/a
On Jan 21, 7:51*am, Robert <rob...@nowhere.xwrote:
Hi,

Is it possible to intercept a call to "instanceof" and return something
different? I wanted to see if it was possible to implement something
No.
like multiple inheritance, especially to use instanceof where a
component extends an object and implements some listener interface too
for a MVC architecture.
Jan 21 '08 #2

P: n/a
Jeremy J Starcher wrote:
On Mon, 21 Jan 2008 13:51:22 +0100, Robert wrote:
>Is it possible to intercept a call to "instanceof" and return something
different? I wanted to see if it was possible to implement something
like multiple inheritance, especially to use instanceof where a
component extends an object and implements some listener interface too
for a MVC architecture.

Javascript does not have OOP as it exists in other languages, you
may need to re-think your goal.
Utter nonsense.

http://javascript.crockford.com/javascript.html
http://en.wikipedia.org/wiki/Prototy...ed_programming

Please also keep your half-witted examples to yourself.
PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
Jan 22 '08 #3

P: n/a
Thomas 'PointedEars' Lahn wrote:
Jeremy J Starcher wrote:
>On Mon, 21 Jan 2008 13:51:22 +0100, Robert wrote:
>>Is it possible to intercept a call to "instanceof" and return something
different?I wanted to see if it was possible to implement something
like multiple inheritance, especially to use instanceof where a
component extends an object and implements some listener interface too
for a MVC architecture.

http://javascript.crockford.com/javascript.html
http://en.wikipedia.org/wiki/Prototy...ed_programming
I have seen your popular examples before of course :)

Anyway, I figured a reasonable way to implement it. I can't use
instanceof, but it is not too bad.

I will do something like this now...

function IMyListener()
{
}

IMyListener.prototype.event = function() {};
IMyListener.__INTERFACE = true;

And when I validate arguments and expect an interface I call an
isImplementation that checks whether the argument object implements all
the methods of an interface.

I still can't use instanceof, which is too bad, but till now I just need
it for arguments validation anyway.

Jan 22 '08 #4

P: n/a
VK
On Jan 22, 5:54 pm, Robert <rob...@nowhere.xwrote:
Anyway, I figured a reasonable way to implement it. I can't use
instanceof, but it is not too bad.

I will do something like this now...

function IMyListener()
{

}

IMyListener.prototype.event = function() {};
IMyListener.__INTERFACE = true;

And when I validate arguments and expect an interface I call an
isImplementation that checks whether the argument object implements all
the methods of an interface.
For a particular property check all along the inheritance chain there
is boolean ('property' in object). So you need only
IMyListener.prototype.event = function() {};
and later then
if ('event' in objectInstance) {
// OK
}

For instanceof and constructor topis in Javascript I regularly
suggest
http://blogs.msdn.com/ericlippert/ar.../06/53352.aspx
by Eric Lippert

Jan 22 '08 #5

P: n/a
VK wrote:
On Jan 22, 5:54 pm, Robert <rob...@nowhere.xwrote:
>Anyway, I figured a reasonable way to implement it. I can't use
instanceof, but it is not too bad.

I will do something like this now...

function IMyListener()
{

}

IMyListener.prototype.event = function() {};
IMyListener.__INTERFACE = true;

And when I validate arguments and expect an interface I call an
isImplementation that checks whether the argument object implements all
the methods of an interface.

For a particular property check all along the inheritance chain there
is boolean ('property' in object). So you need only
IMyListener.prototype.event = function() {};
and later then
if ('event' in objectInstance) {
// OK
}
I don't understand what you are trying to say.
Jan 23 '08 #6

P: n/a
Robert said the following on 1/23/2008 3:40 AM:

<snip>
I don't understand what you are trying to say.
Don't let it bother you, VK doesn't understand what VK says 99% of the time.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jan 23 '08 #7

P: n/a
VK
For a particular property check all along the inheritance chain there
is boolean ('property' in object). So you need only
IMyListener.prototype.event = function() {};
and later then
if ('event' in objectInstance) {
// OK
}

Utter nonsense, any object may have an `event' property. Consider `window'
in MSHTML for a start.
Can you grasp a difference between an approach suggestion and a
turnkey solution? It can be as many necessary checks as needed, OP
simply doesn't provide enough data to code all of them - plus sure he
can easily do it himself.
For instanceof and constructor topis in Javascript I regularly
suggest
http://blogs.msdn.com/ericlippert/ar.../06/53352.aspx
by Eric Lippert
That it is your recommendation is by itself already a good reason to avoid
that resource, however then the author provides sufficient evidence himself
that he has not understood how the prototype chain works
ROTFL
The maker of JScript and WSH engine doesn't know how does the engine
works... Lucky we have Thomas here handy. :-)

First of all Eric Lippert does know it; secondly even if he didn't
know: the engine works in the way he did it, not in the way Thomas or
anyone else may think about it. Do you have any particular complains
of prototype inheritance violations in JScript?
with the following
example code:

var Animal = new Object();
// ...
Reptile.prototype = Animal;
Please _read_ the article, not just look at it. There are some
convoluted code blocks in there with questionable practicality:
because the article explains the theory of .prototype and .constructor
properties in Javascript, it is not some code snippet with comments.
Jan 23 '08 #8

P: n/a
VK wrote:
>>For a particular property check all along the inheritance chain there
is boolean ('property' in object). So you need only
IMyListener.prototype.event = function() {};
and later then
if ('event' in objectInstance) {
// OK
}
Utter nonsense, any object may have an `event' property. Consider `window'
in MSHTML for a start.

Can you grasp a difference between an approach suggestion and a
turnkey solution? [...]
Yes, I can. So, unsurprisingly, your suggestion is a bad one again. Why
don't you simply stop suggesting until you have understood how things work?
>>For instanceof and constructor topis in Javascript I regularly
suggest
http://blogs.msdn.com/ericlippert/ar.../06/53352.aspx
by Eric Lippert
>That it is your recommendation is by itself already a good reason to avoid
that resource, however then the author provides sufficient evidence himself
that he has not understood how the prototype chain works

ROTFL
The maker of JScript and WSH engine doesn't know how does the engine
works... [...]
If what you say about him is true, apparently he does not know how he was
supposed to implement the language specification. That would explain a
great deal about JScript, though.
PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
Jan 23 '08 #9

P: n/a
VK
ROTFL
The maker of JScript and WSH engine doesn't know how does the engine
works... [...]

If what you say about him is true, apparently he does not know how he was
supposed to implement the language specification. That would explain a
great deal about JScript, though.
http://blogs.msdn.com/ericlippert/about.aspx
also feel free to check out in Microsoft or ask Eric himself in this
blog, he is a nice guy.

So I guess I have to repeat my question: do you have any theoretical
complains against the prototype inheritance in JScript?
So far from the point of view of the source trust whatever Thomas says
is pointless as long as it contradicts to what Eric says. IMO it is a
reasonable choice for anyone.

Jan 23 '08 #10

P: n/a
VK wrote:
>>ROTFL
The maker of JScript and WSH engine doesn't know how does the engine
works... [...]
If what you say about him is true, apparently he does not know how he was
supposed to implement the language specification. That would explain a
great deal about JScript, though.

http://blogs.msdn.com/ericlippert/about.aspx
also feel free to check out in Microsoft or ask Eric himself in this
blog, he is a nice guy.

So I guess I have to repeat my question: do you have any theoretical
complains against the prototype inheritance in JScript?
So far from the point of view of the source trust whatever Thomas says
is pointless as long as it contradicts to what Eric says. IMO it is a
reasonable choice for anyone.
Unsurprisingly, you missed the point completely. Eric's explanation there
is probably correct (I have not read it thoroughly yet, what I read was
enough to see his mistake), however he only observed what he observe because
he did not implement prototype-based inheritance properly in his example
code although he should have known better. What's worse, he attributes this
behavior to a lacking definition in the ECMAScript specification although he
should have known better.
PointedEars
Jan 23 '08 #11

P: n/a
VK
On Jan 24, 2:21 am, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
Eric's explanation there
is probably correct (I have not read it thoroughly yet <snip>
So maybe you read it first and then we can discuss all his mistakes?
If any found it will be sure possible to counter-proof them with some
experimental code.
Jan 23 '08 #12

P: n/a
VK
On Jan 24, 2:21 am, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
Eric's explanation there
is probably correct (I have not read it thoroughly yet <snip>

Nice distortion job.
Why "distortion job"? I just follow your own suggestion "quote only
what is relevant for your post". The relevant part is that you didn't
read the article you are trying to criticize, anything else is
irrelevant. :-) :-|
So maybe you read it first and then we can discuss all his mistakes?
If any found it will be sure possible to counter-proof them with some
experimental code.

You are an idiot. Probably someone has called you so before.
Extremely productive argument. So I guess you don't have any
theoretical complains against prototype inheritance as implemented in
JScript engine. Well, this branch of discussion may be closed then?
Jan 24 '08 #13

P: n/a
VK wrote:
>>On Jan 24, 2:21 am, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
Eric's explanation there
is probably correct (I have not read it thoroughly yet <snip>
Nice distortion job.

Why "distortion job"? I just follow your own suggestion "quote only
what is relevant for your post". The relevant part is that you didn't
read the article you are trying to criticize, anything else is
irrelevant. :-) :-|
>>So maybe you read it first and then we can discuss all his mistakes?
If any found it will be sure possible to counter-proof them with some
experimental code.
You are an idiot. Probably someone has called you so before.

Extremely productive argument. So I guess you don't have any
theoretical complains against prototype inheritance as implemented in
JScript engine. Well, this branch of discussion may be closed then?

Thank you guys for making me laugh this morning :D
Jan 24 '08 #14

This discussion thread is closed

Replies have been disabled for this discussion.