Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Builtin object prototypes

Reply
Thread Tools

Builtin object prototypes

 
 
Ari Krupnik
Guest
Posts: n/a
 
      11-27-2006
scripts can add methods to the prototypes of builtin objects in
JaavScript. I can assign functions to String.prototype.*, for
instance. I want to add a method to Node, but when I try to execute
the following IE says "'Node' is undefined." Mozilla works as I
expected it to. Is Node called something else in IE? Does IE not allow
manipulating the prototypes of some builtin objects?

Node.prototype.nt = function() {
return this.nodeType;
}

Ari.

--
Elections only count as free and trials as fair if you can lose money
betting on the outcome.
 
Reply With Quote
 
 
 
 
VK
Guest
Posts: n/a
 
      11-27-2006

Ari Krupnik wrote:
> scripts can add methods to the prototypes of builtin objects in
> JaavScript. I can assign functions to String.prototype.*, for
> instance. I want to add a method to Node, but when I try to execute
> the following IE says "'Node' is undefined." Mozilla works as I
> expected it to.


Well, actually Mozilla works in - maybe convenient in some
circumstances - but non-expected way.
prototype is property of a JavaScript object; DOM Node is not a
JavaScript object and it has nothing to do with say Object()
constructor (same way as say DIV element has nothing to do with Array


IE exposes the base [element] interface via behavior mechanics. This
way you can make say each <p> element to have AJAX interface or each
<div> acting as media player. But in case of Node I really don't know
what to suggest as it is not an element in (X)HTML / XML sense, it is
unit one level below. Are you trying to uniformly augment every single
element on your page? What is your actual aim? (so maybe some solution
is possible).

 
Reply With Quote
 
 
 
 
RobG
Guest
Posts: n/a
 
      11-27-2006

Ari Krupnik wrote:
> scripts can add methods to the prototypes of builtin objects in
> JaavScript. I can assign functions to String.prototype.*, for
> instance. I want to add a method to Node, but when I try to execute
> the following IE says "'Node' is undefined." Mozilla works as I
> expected it to. Is Node called something else in IE? Does IE not allow
> manipulating the prototypes of some builtin objects?
>
> Node.prototype.nt = function() {
> return this.nodeType;
> }


There is a base object for the DOM that implements the Node interface,
in Gecko browsers it's called "Node" and you can mess with the
prototype.

IE doesn't have a Node object, though you can extend DOM objects to
some extend using behaviours:

<URL:
http://msdn.microsoft.com/library/de...o/creating.asp
>



--
Rob

 
Reply With Quote
 
Richard Cornford
Guest
Posts: n/a
 
      11-27-2006

"Ari Krupnik" <> wrote in message
news:...
> scripts can add methods to the prototypes of builtin objects in
> JaavScript. I can assign functions to String.prototype.*, for
> instance. I want to add a method to Node, but when I try to execute
> the following IE says "'Node' is undefined." Mozilla works as I
> expected it to. Is Node called something else in IE? Does IE not allow
> manipulating the prototypes of some builtin objects?


The Node is not a "built in object", it is a "host object" (or at least,
all objects implementing the Node interface in browser DOMs are host
objects), and host objects are not required to facilitate modification
of their prototypes, expose prototypes (even have prototypes) or expose
constructors.

As a result some hosts may provide those facilities (as Mozilla does)
and others may not (like IE). Both alternatives (and everything in
between) are completely in accordance with the javascript language
specification, and no other specifications (such as the W3C DOM, and its
ECMAScript bindings) has attempted to apply any additional constraints
on host objects in the web browser context.

Richard.


 
Reply With Quote
 
Ari Krupnik
Guest
Posts: n/a
 
      11-28-2006
"VK" <> writes:

> Ari Krupnik wrote:
>> scripts can add methods to the prototypes of builtin objects in
>> JavaScript. I can assign functions to String.prototype.*, for
>> instance. I want to add a method to Node, but when I try to execute
>> the following IE says "'Node' is undefined." Mozilla works as I
>> expected it to.

>
> Well, actually Mozilla works in - maybe convenient in some
> circumstances - but non-expected way.


I said "the way I expected it to," being fully aware that my
expectation may not be grounded in any reality :=)

> Are you trying to uniformly augment every single
> element on your page? What is your actual aim? (so maybe some solution
> is possible).


I have a bunch of functions that provide XPath-style navigation of the
DOM. Functions like followingSibling(node, predicate) that find the
closest sibling in document order for which the function 'predicate'
returns true. Also functions like textValue(node) that concatenates
all character data within a node. Most of these are not specific to
Element, e.g. textValue() of TextNode is its nodeValue. I wanted to
make these functions methods of Node so I could call them as
node.textValue() instead of textValue(node).

It's not that big of a deal. Thanks for explaining the difference
between String and Node to me. I had not internalized the difference
between builtin types and host objects.

Ari.

--
Elections only count as free and trials as fair if you can lose money
betting on the outcome.
 
Reply With Quote
 
RobG
Guest
Posts: n/a
 
      11-28-2006

David Golightly wrote:
[...]
> No, what you're looking to do isn't possible directly, but you could
> write wrappers that would give you the interface you're looking for.
> See for instance jQuery, which is basically a wrapper framework; also,
> if you want to use XPath you can use XPath directly - though IE doesn't
> expose XPath for HTML documents, there's an LGPL implementation:
>
> http://js-xpath.sourceforge.net/
>
> which tricks IE into thinking the currently loaded HTML document is an
> MSXML-loaded document. And Firefox allows XPath natively with HTML.
> (Unfortunately there's no easy solution for Safari and Opera.)


The WebKit open source project started releasing XPath support in Aug
06[1], it should be picked up by Safari with OS X 10.5.

That puts Safari more-or-less on par with IE in terms of XPath support,
but unfortunately keeps XPath as practically useless on the web for
another couple of years.

It should start to to be viable for intranet or specialist web
applications though.

1. <URL: http://webkit.org/blog/?p=65 >


--
Rob

 
Reply With Quote
 
Ari Krupnik
Guest
Posts: n/a
 
      11-28-2006
"David Golightly" <> writes:

> Ari Krupnik wrote:
>>
>> I have a bunch of functions that provide XPath-style navigation of the
>> DOM. Functions like followingSibling(node, predicate) that find the
>> closest sibling in document order for which the function 'predicate'
>> returns true. Also functions like textValue(node) that concatenates
>> all character data within a node. Most of these are not specific to
>> Element, e.g. textValue() of TextNode is its nodeValue. I wanted to
>> make these functions methods of Node so I could call them as
>> node.textValue() instead of textValue(node).

>
> if you want to use XPath you can use XPath directly - though IE doesn't
> expose XPath for HTML documents, there's an LGPL implementation:
>
> http://js-xpath.sourceforge.net/
>
> which tricks IE into thinking the currently loaded HTML document is an
> MSXML-loaded document.


How does it do that? When I need to run an XSLT transformation on an
HTML document in IE, I walk the HTML DOM and construct an X(HT)ML DOM
based on it that IE can transform natively. Are you saying there is a
way to make IE believe that an MSHTML document is an MSXML document
with corresponding methods on its nodes, without copying it over?

Ari.

--
Elections only count as free and trials as fair if you can lose money
betting on the outcome.
 
Reply With Quote
 
Ari Krupnik
Guest
Posts: n/a
 
      11-28-2006
"David Golightly" <> writes:

> On looking into it a little further, it appears that Opera, much like
> IE, has XPath support, but only for XML documents and not HTML. Unlike
> IE, fortunately, it's W3DOM compliant.


Could you explain the difference, and how being compliant is
unfortunate? I have no experience with client-side XSLT outside of IE
and FF.

> However, for those with huge Ajax apps who don't mind the bandwidth
> loss in exchange for the flexibility of XPath, Google's got an
> XSLT/XPath implementation entirely in JavaScript which works in all
> modern browsers and will if nothing else prove an interesting study:
> http://code.google.com/p/ajaxslt/


I thought the way ajaxslt is implemented in browsers without native
XSLT support was by sending a serialized document to a server,
parsing and transforming it there, then serializing it again, sending
it back and parsing it again on the client. Was I wrong? That sounds a
bit complicated for finding the character data content of a node,
which is the type of processing that I need :=)

Ari.

--
Elections only count as free and trials as fair if you can lose money
betting on the outcome.
 
Reply With Quote
 
RobG
Guest
Posts: n/a
 
      11-28-2006

Ari Krupnik wrote:
> "David Golightly" <> writes:
>
> > On looking into it a little further, it appears that Opera, much like
> > IE, has XPath support, but only for XML documents and not HTML. Unlike
> > IE, fortunately, it's W3DOM compliant.

>
> Could you explain the difference, and how being compliant is
> unfortunate?


You misunderstood: David is saying that that Opera's compliance is
fortunate (i.e. a good thing).


--
Rob

 
Reply With Quote
 
VK
Guest
Posts: n/a
 
      11-28-2006

Ari Krupnik wrote:
> I have a bunch of functions that provide XPath-style navigation of the
> DOM. Functions like followingSibling(node, predicate) that find the
> closest sibling in document order for which the function 'predicate'
> returns true. Also functions like textValue(node) that concatenates
> all character data within a node. Most of these are not specific to
> Element, e.g. textValue() of TextNode is its nodeValue. I wanted to
> make these functions methods of Node so I could call them as
> node.textValue() instead of textValue(node).
>
> It's not that big of a deal. Thanks for explaining the difference
> between String and Node to me.


You are welcome

In relevance to your original question you may indeed go for behavior
(please note that in this case US English spelling is implied because
it's the personal method name, not just a dictionary entry). As it's an
early mosning and I'm going to sleep now, I grapped the first .htc file
I found on my disk, just to explain the concept. By Murphy law it
happened to be the ugliest ill-formed crap from tmp dumps But it
seems working for IE up to level to show what do I mean.

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<style type="text/css">
body * {
behavior: url(hover.htc);
}
</style>
</head>
<body>
<p>P</p>
<address>ADDRESS</address>
<div>DIV</div>
</body>
</html

Where hover.htc is:

<PUBLIC:ATTACH EVENT="onmouseover" ONEVENT="Hilite()" />
<PUBLIC:ATTACH EVENT="onmouseout" ONEVENT="Restore()" />
<SCRIPT LANGUAGE="JScript">
function Hilite() {
if (event.srcElement == element) {
innerHTML = "Mouse is OVER me";
}
}

function Restore() {
if (event.srcElement == element) {
innerHTML = "Mouse is OUT of me";
}
}
</SCRIPT>

Now each and every HTML element on your page became mouse listener by
default (hover to see it).

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
questions (& answers) about object, type, builtin types, class,metaclass and __getattribute__ Amirouche B. Python 5 08-23-2011 08:47 PM
subtle error slows code by 10x (builtin sum()) - replace builtin sumwithout using import? bdb112 Python 2 07-02-2011 03:13 AM
static class object - are builtin members 0? Stephen Howe C++ 1 05-21-2011 11:17 PM
Variables and Object creation using prototypes nrlz@hotmail.com Javascript 6 04-30-2006 09:08 PM
Object Prototypes Arthaey Angosii Javascript 1 08-12-2005 10:36 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57