473,739 Members | 4,464 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can/should one add expando properties to host-supplied objects?

Should dynamic ("expando") properties be restricted to native and
user-defined objects? Or should host objects - such as references to
the browser or a plug-in or to the document and its elements - also
allow them?

Adding (and removing) object properties dynamically is an acceptable
and common practice in JavaScript, and greatly adds to the power and
character of the language. Essentially, an object in JavaScript can be
considered to be a collection of name-value pairs that define its
properties (including methods, as function-valued properties), and one
is usually free to add to this properties list at will.

Well, that is certainly the case for native (built-in) objects, such as
those of type Object, Number, String, Array, Date, etc; also for any
objects created from user-defined (programmer-defined) constructor
functions.

But I have never been certain if it was advisable, or even "legal", to
add properties to objects created by the host, such as the document
object or an HTML element or an XML node.

(Trivial example: document.myNewP rop = 1234; )

Obviously, the actual host-supplied object - presumably, an instance of
a class written in C++ or Java or a similar statically, strongly typed,
compiled language - cannot have new properties, such as myNewProp,
added at run-time. But I have always understood - correct me if I am
wrong - that the JavaScript interpreter would automatically create a
second object, on the JavaScript side of the DOM interface, as a proxy.
That is, a JavaScript-language proxy object would map onto the
host-language class-based object. The new property ("myNewProp" ) would
be added only to that proxy object, as just another name/value pair,
and not to the host object, which would know nothing of the addition.

Well that is the mental image I had of the process behind the scenes.

Adding expando properties does seems fairly common practice, and I have
succeeded with this practice until now, but that does not prove much;
perhaps the host programs and/or JavaScript interpreters/engines have
just been too lax and accomodating.

The reason for this message is that my luck has finally run out, with
the Batik/Squiggle SVG viewer (which I believe runs the Mozilla Rhino
JavaScript engine, written in Java) and I am seeking a resolution of
the problem.

I had been developing some interactive, scripted SVG applications, and
had added extra data, in the form of expando properties, to some SVG
element objects (<rect> elements, for example), as a convenient way of
attaching metadata directly to them, rather than having to keep track
of separate JavaScript arrays or collections of data. This approach
worked in ASV3 and ASV6 (Adobe SVG viewer plug-ins, versions 3 and 6)
within both IE6 and Mozilla Firefox 1.0. (running on a Windows 2000 OS
PC) But not in Batik 1.5.

I have drastically simplified the example SVG file to the following
rather trivial test-case:
=============== =============== =============== =============== =============== =============== ============

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN"
"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">

<svg xmlns="http://www.w3.org/2000/svg" version="1" width="100%"
height="100%" onload="init(ev t)">

<script><![CDATA[
function init(evt) {
var doc = evt.target.owne rDocument;
var rct = doc.getElementB yId("rct1");
rct.newProp = 1234;
alert("rct.newP rop=" + rct.newProp);
}
]]></script>

<rect id="rct1" width="200" height="100" style="fill:red " />

</svg>

=============== =============== =============== =============== =============== =============== ============

This works as intended in the Adobe plug-in in IE6 and in Mozilla. But
in Batik/Squiggle I got this error message:

org.mozilla.jav ascript.Evaluat orException: Java class
"org.apache.bat ik.dom.svg.SVGO MRectElement"
has no public instance field or method named "newProp".
This suggests that the JavaScript interpreter is trying to add -
without any proper error trapping - the "newProp" property to the host
object (of Java class SVGOMRectElemen t, which implements the SVG DOM
SVGRectElement interface), instead of to a proxy object on the
JavaScript side of the SVGRectElement interface.

Is it reasonable to regard this as a bug, or unwarranted restriction,
on the part of Batik? Or is Batik merely holding firm to a standard
that states one should not try to add properties to script references
to host-created objects?

Although this example features an XML/SVG document, I feel that this
issue is one that applies to JavaScript in general, as JavaScript is
always used in a context of some sort (web browser, SVG viewer or
plugin, HTML or XML DOM, and so on) which exposes its own objects, at
least indirectly, to the scripting language.

Jul 23 '05 #1
3 3029


ytrewq wrote:

But I have never been certain if it was advisable, or even "legal", to
add properties to objects created by the host, such as the document
object or an HTML element or an XML node.

(Trivial example: document.myNewP rop = 1234; )


Let's look into the ECMAScript specification (Standard ECMA-262 edition
3) to try to answer that:

Section 4 gives an overview of the types, it defines the Object type in
4.3.3 and then goes on to distinguish Native Objects (4.3.6), Built-in
Objects (4.3.7), and Host Objects (4.3.8).
Object is defined as follows: "An object is a member of the type Object.
It is an unordered collection of properties each of which
contains a primitive value, object, or function. A function stored in a
property of an object is called a
method."
Host Object is defined as follows: "Host Object
A host object is any object supplied by the host environment to complete
the execution environment of
ECMAScript. Any object that is not native is a host object."

So in scripting of HTML documents in browsers the document object for
instance is an host object.

Now to the section that deals with objects and their properties in
detail, it is section 8.6 The Object Type. Section 8.6.2 Internal
Properties and Methods includes the formal definition of read (get) or
write (put) access to a property. It has a paragraph that clearly says:
"The following table summarises the internal properties used by this
specification. The description
indicates their behaviour for native ECMAScript objects. Host objects
may implement these internal
methods with any implementation-dependent behaviour, or it may be that a
host object implements only
some internal methods and not others."

On the other hand the formal methods
[[Get]] (PropertyName) Returns the value of the property.
[[Put]] (PropertyName, Value) Sets the specified property to Value.
are listed and it then says
"Every object (including host objects) must implement the
[[Prototype]] and [[Class]] properties and the
[[Get]], [[Put]], [[CanPut]], [[HasProperty]], [[Delete]], and
[[DefaultValue]] methods."
but goes on to explain
"For native objects the [[Get]], [[Put]], [[CanPut]],
[[HasProperty]], [[Delete]] and [[DefaultValue]]
methods behave as described in described in 8.6.2.1, 8.6.2.2, 8.6.2.3,
8.6.2.4, 8.6.2.5 and 8.6.2.6,"
and "Host objects may implement these methods in any manner unless
specified otherwise;"

Thus looking at that an implementation is not required to have [[Put]]
implemented on host objects the way it usually works on native objects
where you can create a new property and set its value with
object.property Name = expression
an host object could throw an exception on such attempts.

This is for instance the case with JScript on Windows when you create
and MSXML xmlDocument e.g.

var xmlDocument = new ActiveXObject(' Microsoft.XMLDO M');
try {
xmlDocument.myN ewProp = 1234;
}
catch (e) {
alert(e.message );
}

and you get an error alike "The object doesn't support this property or
method".

On the other hand it is of course desirable to be able to add properties
to host objects and usually in browser when scripting HTML this works.
As for Batik and Rhino I can't tell whether it is because of the way
Rhino principally sets up Java host objects or whether the Batik
implementors have for whatever reasons (maybe as they use some metha
scripting framework where JavaScript with Rhino is only one option)
choosen to use Rhino that way in Batik. I would guess that you are more
likely to find an answer on that in a Batik group or developer mailing list.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #2


Martin Honnen wrote:

On the other hand it is of course desirable to be able to add properties
to host objects and usually in browser when scripting HTML this works.
As for Batik and Rhino I can't tell whether it is because of the way
Rhino principally sets up Java host objects


I have had a look at Rhino as documented on www.mozilla.org and I think
if you really set up a Java class as a JavaScript host object as
documented here
<http://www.mozilla.org/rhino/tutorial.html#J avaScriptHostOb jects>
then it is possible to add properties as you like when scripting the
object with JavaScript, for instance I have compiled the simple Java
class Counter and then loaded it into the Rhino shell, created a Counter
object and them added all sorts of properties with JavaScript.
Thus if the objects exposed to script in the Batik viewer do not take
properties then it is likely that they are not set up as host objects
but are simply Java objects directly exposed to JavaScript as Rhino allows.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #3
Thank you, Martin, for these replies and for the research you have
carried out into Rhino.

Cheers!

Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3781
by: Fred Chen | last post by:
Hi, I've created a custom component. I want one of the properties of my custom component to be the output file name which is found in project properties. I know that I can get this by writing a macro, but I would prefer doing this in my component's constructor. Currently I am trying to use ActiveSolutionProjects in the EnvDTE object to obtain the active project but it always returns an empty object.
2
11255
by: SACHIN | last post by:
I have this class as part of a Consol application. using System; namespace Bugreport { /// <summary> /// This class tries to use the Class/Struct combination. /// </summary> class Class1 {
3
13713
by: Guy Harwood | last post by:
Hi, I have designed a textbox that inherits from the System.Windows.Forms.Textbox control. when the control is readonly the back color changes to a light blue to indicate that it is frozen. This all works well, but i would like the color to be configurable via the app.config file.
10
7366
by: Sunny | last post by:
Hi, I have an old problem which I couldn't solve so far. Now I have found a post in that group that gave me an idea, but I can not fully understand it. The problem is: I'm trying to use a Windows.Forms.UserControl in a COM environment, i.e. I want to host that control in a COM host. So far, so good, I can host it, but I can not reach the parent COM object from the control (Parent property is null :( ). I have stopped the control in the...
6
5182
by: JerryP | last post by:
Hello, is there a way to launch the property dialogue for a directory from my c# app ? I would also like to launch the User Account Properties from Active Directory Users and Computers, and the properties window for an Object in Active Directory. Thanks for any hints.
4
9378
by: james | last post by:
I cannot get my UserControl's browsable properties to show up in the designer properties panel. I have then public virtual bool TestProp { {get return testProp; } set { testProp = value; } } but it doesn't show up in the designer. By the way, this used to work in the 2002 version, now in 2003 I am getting all kinds of problems like this
4
2445
by: TS | last post by:
When i try to access the page class or parent properties inside a server control that is nested in another server control, they are null. I don't even call CreateChildControls for the parent control until its pre-render event, so i know the parent control is on the page. Any ideas? thanks
15
3712
by: Mike Caputo | last post by:
I'm trying to figure out how to use this class/interface to dynamically create properties. I've looked at all the documentation, but I can't find any real sample code, and I'm really not sure how to get started. Does anyone have either a self-written example or a link to one? I'd appreciate it. Mike --
0
1172
by: Nick L | last post by:
Here is the PowerShell script I've been working on (.NET question is at the bottom): ### setup SQL connection $conn = new-object System.Data.SqlClient.SqlConnection $conn.ConnectionString = "server=*****;database=****;UID=*****;PWD=*****" ### setup SQL command $cmd = new-object System.Data.SqlClient.SqlCommand ### Get SQL commands from text file
5
1607
by: Krice | last post by:
There is a component class inside the host class: class Host { Component *c; .... In some cases Component needs to call the host and I guess the only way is that you pass the host for it:
0
9483
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9269
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9211
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8216
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6056
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4572
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4826
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3282
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2748
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.