473,803 Members | 2,279 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Defining a property with the DontEnum attribute

All --

I'm trying to solve a problem for which I think the solution will be
to *cheat*; but I don't mind doing so for this case. The background is:

Given an object constructor, and an instance

SampleObj = function() {
this.prop = 1;
}
obj = new SampleObj();
obj has one enumerable property: 'prop'; and several builtin,
non-enumerable properties as well: 'toString', 'hasOwnProperty ',
'propertyIsEnum erable', etc. If I redefined any of those, e.g:

SampleObj.proto type.toString = function() {return '[SampleObj]'};

then obj still has only one enumerable property; the redefinition of
toString doesn't affect its "DontEnum"-ness.

I'd like to be able to create such a property from JavaScript. In
particular, in older versions of Safari, some of those required properties
aren't implemented (hasOwnProperty , isPrototypeOf, propertyIsEnume rable,
& toLocaleString) . If I define them myself:

Object.prototyp e.toLocaleStrin g = Object.prototyp e.toString;
[etc.]

then they are enumerable, which causes problems for code like

for (var prop in obj) ...

I'll get a different set of enumerated properties in Safari than in (e.g.)
Mozilla; and have to specifically check for those that I defined myself,
to make code work appropriately on each browser. This gets tricky
(considering that the old Safari versions also failed to implement
toExponential, toFixed and toPrecision for Number objects).

The ability to set the DontEnum and ReadOnly attributes of object
properties would have been great; but (afaik) there is no way to do so. So
I'm looking for a "backdoor" in Safari that would allow this.

Help anyone?
Howard Jess
Nov 23 '05 #1
16 4351
VK

Howard Jess wrote:
All --

I'm trying to solve a problem for which I think the solution will be
to *cheat*; but I don't mind doing so for this case. The background is:

Given an object constructor, and an instance

SampleObj = function() {
this.prop = 1;
}
obj = new SampleObj();
obj has one enumerable property: 'prop'; and several builtin,
non-enumerable properties as well: 'toString', 'hasOwnProperty ',
'propertyIsEnum erable', etc. If I redefined any of those, e.g:

SampleObj.proto type.toString = function() {return '[SampleObj]'};

then obj still has only one enumerable property; the redefinition of
toString doesn't affect its "DontEnum"-ness.

I'd like to be able to create such a property from JavaScript. In
particular, in older versions of Safari, some of those required properties
aren't implemented (hasOwnProperty , isPrototypeOf, propertyIsEnume rable,
& toLocaleString) . If I define them myself:

Object.prototyp e.toLocaleStrin g = Object.prototyp e.toString;
[etc.]

then they are enumerable, which causes problems for code like

for (var prop in obj) ...

I'll get a different set of enumerated properties in Safari than in (e.g.)
Mozilla; and have to specifically check for those that I defined myself,
to make code work appropriately on each browser. This gets tricky
(considering that the old Safari versions also failed to implement
toExponential, toFixed and toPrecision for Number objects).

The ability to set the DontEnum and ReadOnly attributes of object
properties would have been great; but (afaik) there is no way to do so. So
I'm looking for a "backdoor" in Safari that would allow this.

Help anyone?


if ("propertyNa me" in myObject)
gives you [true] if propertyName is presented either in the object
itself or anywhere in its prototype chain ab obo.

if (myObject.hasOw nProperty("prop ertyName"))
gives you [true] only if propertyName is presented in the object itself
(prototype chain is disregarded)

So it is rather simple task. But if under "older Safari" you mean
anything below 2.0 then I dubt very much that these methods are
presented themselve - and even if they are - that they will work as
they should.
Safari 1.x series can barely recognize HTML itself, what
hasOwnProperty, forget it! :-)
You may try anyway.

Nov 23 '05 #2
Howard Jess wrote:
SampleObj = function() {
this.prop = 1;
}
obj = new SampleObj();
obj has one enumerable property: 'prop'; and several builtin,
non-enumerable properties as well: 'toString', 'hasOwnProperty ',
'propertyIsEnum erable', etc. If I redefined any of those, e.g:

SampleObj.proto type.toString = function() {return '[SampleObj]'};

then obj still has only one enumerable property; the redefinition of
toString doesn't affect its "DontEnum"-ness.

I'd like to be able to create such a property from JavaScript.
You cannot do this.
[...]
The ability to set the DontEnum and ReadOnly attributes of object
properties would have been great; but (afaik) there is no way to
do so. So I'm looking for a "backdoor" in Safari that would allow
this.


As for DontEnum:

Object.prototyp e.toLocaleStrin g.dontEnum = true;

for (var foo in obj)
{
if (!obj[foo].dontEnum)
{
// ...
}
}

Should work everywhere, not only in Safari.

As for ReadOnly, there is no way to implement something
alike in JavaScript < 2.0/JScript < 7.0/ECMAScript < 4.
HTH

PointedEars
Nov 23 '05 #3
Thomas 'PointedEars' Lahn wrote:
SampleObj = function() {
this.prop = 1;
}
obj = new SampleObj();
obj has one enumerable property: 'prop'; and several builtin,
non-enumerable properties as well: 'toString', 'hasOwnProperty ',
'propertyIsEnum erable', etc. If I redefined any of those, e.g: [snip]
I'd like to be able to create such a property from JavaScript.
You cannot do this.


Yes, as I say right here:
[...]
The ability to set the DontEnum and ReadOnly attributes of object
properties would have been great; but (afaik) there is no way to
do so. So I'm looking for a "backdoor" in Safari that would allow
this.

By "backdoor", I mean some unorthodox, undocumented hook into Safari's
JS engine. I understand that this is not possible per the spec.
As for DontEnum:

Object.prototyp e.toLocaleStrin g.dontEnum = true;

for (var foo in obj)
{
if (!obj[foo].dontEnum)
{
// ...
}
}

Should work everywhere, not only in Safari.


Yes, of course, if I want to change *every* "for .. in" loop in every
piece of JavaScript code I deal with; but this is still unsatisfactory;
the "dontEnum" property you propose is itself now an enumerable property
of toLocaleString.

To repeat: I'm looking for a "backdoor", "cheat", "hook" (invent your
own term) to allow a property that I define to be non-enumerable.

hj
Nov 23 '05 #4
Howard Jess wrote:
Thomas 'PointedEars' Lahn wrote:
[...]
The ability to set the DontEnum and ReadOnly attributes of object
properties would have been great; but (afaik) there is no way to
do so. So I'm looking for a "backdoor" in Safari that would allow
this.

By "backdoor", I mean some unorthodox, undocumented hook into Safari's
JS engine. I understand that this is not possible per the spec.
[...]
Yes, of course, if I want to change *every* "for .. in" loop in every
piece of JavaScript code I deal with; but this is still unsatisfactory;


Tough luck.
the "dontEnum" property you propose is itself now an enumerable property
of toLocaleString.
True. Why would that pose a problem in the specific case?
To repeat: I'm looking for a "backdoor", "cheat", "hook" (invent your
own term) to allow a property that I define to be non-enumerable.


Write your own script engine and call the UA that uses it "Safari" :)

Seriously, I do not understand why anyone would need this, so I did
not gave it any thought. That said, Safari uses KJS which is Open
Source software. You could dig through the source code to find a
leak. I do not know one, and I do think this is hardly on-topic here.
PointedEars
Nov 23 '05 #5
Howard Jess wrote:
Thomas 'PointedEars' Lahn wrote:

SampleObj = function() {
this.prop = 1;
}
obj = new SampleObj();
obj has one enumerable property: 'prop'; and several builtin,
non-enumerable properties as well: 'toString', 'hasOwnProperty ',
'propertyIsE numerable', etc. If I redefined any of those, e.g:
[snip]
I'd like to be able to create such a property from JavaScript.


You cannot do this.

Yes, as I say right here:
[...]
The ability to set the DontEnum and ReadOnly attributes of object
properties would have been great; but (afaik) there is no way to
do so. So I'm looking for a "backdoor" in Safari that would allow
this.

By "backdoor", I mean some unorthodox, undocumented hook into Safari's
JS engine. I understand that this is not possible per the spec.

As for DontEnum:

Object.prototyp e.toLocaleStrin g.dontEnum = true;

for (var foo in obj)
{
if (!obj[foo].dontEnum)
{
// ...
}
}

Should work everywhere, not only in Safari.

Yes, of course, if I want to change *every* "for .. in" loop in every
piece of JavaScript code I deal with; but this is still unsatisfactory;
the "dontEnum" property you propose is itself now an enumerable property
of toLocaleString.

To repeat: I'm looking for a "backdoor", "cheat", "hook" (invent your
own term) to allow a property that I define to be non-enumerable.


Sometimes it is better to describe more generally what you are trying to
do, rather be specific about a particular solution. Asking for hacks
into a specific JS implementation is unlikely to be answered in a public
forum - though Apple may be keen to encourage responses :-)

You could put your values into a data object inside your object (kinda
like a data fork in older Mac-speak):

SampleObj = function() {
this.data = {};
this.data.prop = 1;
}
obj = new SampleObj();

for (prop in obj.data) {
// ...
}

--
Rob
Nov 23 '05 #6
Rob --
I'd like to be able to create such a property from JavaScript.


To repeat: I'm looking for a "backdoor", "cheat", "hook" (invent your
own term) to allow a property that I define to be non-enumerable.


Sometimes it is better to describe more generally what you are trying to
do, rather be specific about a particular solution. Asking for hacks
into a specific JS implementation is unlikely to be answered in a public
forum - though Apple may be keen to encourage responses :-)

You could put your values into a data object inside your object (kinda
like a data fork in older Mac-speak):

SampleObj = function() {
this.data = {};
this.data.prop = 1;
}
obj = new SampleObj();

for (prop in obj.data) {
// ...
}


Fair enough. I have a code library that corrects some browsers's Javascript
misbehavior, and implements missing features. E.g.

var sel = document.create Element('select ');
var opt = document.create Element('option ');
sel.add(opt,nul l);

This doesn't work as is on Internet Explorer (which expects either a
single argument, or an index rather than an object reference for the second
argument); the library (with some help) makes it work.

Similarly,

var obj = {thing:1, otherThing:'abc '};
if (obj.hasOwnProp erty('lastThing ')) ...

doesn't work in Safari 1.2: that browser doesn't implement hasOwnProperty,
among others.

It's simple enough to implement it and the rest; e.g.:

Object.prototyp e.hasOwnPropert y = function(propNa me) {
...
}

But in a library, I have no control over user code. And defining it
this way breaks every "for .. in" loop, whose code now must be aware
of the hasOwnProperty in -every- object. *THAT'S* why I'd like to be
able to define a new "DontEnum" property. And yes, I understand it's
not possible in standard JavaScript, and yes, I've begun looking
through the sources; I was hoping someone who frequents this group may
have some insight that might save me some time.

Thanks for any help or suggestions.
hj
Nov 24 '05 #7
Howard Jess wrote:
Rob --
Instead of strangely addressing people directly when posting in
an m:n medium like Usenet where everybody may read and post, you
should provide attribution of quoted material and trim your
quotes as described in <jibbering.co m/faq/faq_notes/pots1.html>.
[...] I have a code library that corrects some browsers's
Javascript misbehavior, and implements missing features. E.g.

var sel = document.create Element('select ');
var opt = document.create Element('option ');
sel.add(opt,nul l);

This doesn't work as is on Internet Explorer (which expects either a
single argument, or an index rather than an object reference for the
second argument); the library (with some help) makes it work.
Why,

sel.options[sel.options.len gth] = new Option(...);

from DOM Level 0 still suffices everywhere and every time, so there is
no need for "repair". Please, prove me wrong.
Similarly,

var obj = {thing:1, otherThing:'abc '};
if (obj.hasOwnProp erty('lastThing ')) ...

doesn't work in Safari 1.2: that browser doesn't implement hasOwnProperty,
among others.
So? Beyond my ObjectInspector , I cannot think of a single instance where
it was needed to use hasOwnProperty( ). Testing with simple conditional
expressions and the `typeof' operator has sufficed in all other cases.
Especially for methods, where hasOwnProperty( ) does not suffice to
determine if there is a high probability that the property can be called;
I'd like to have a hasOwnMethod() but there is no such method.
It's simple enough to implement it and the rest; e.g.:

Object.prototyp e.hasOwnPropert y = function(propNa me) {
...
}

But in a library, I have no control over user code.
That is something you have to be aware of when coding for a host
environment. No surprise here.
And defining it this way breaks every "for .. in" loop, whose code now
must be aware of the hasOwnProperty in -every- object.


I repeat: Show a _specific_ case where that would break and someone will
probably be able point out how to work around it. The very approach of a
general library that handles every possible situation regarding browser
scripting is inevitably a futile one. Recent discussions clearly have
pointed this out, and I strongly suggest you waste no more time on this.

Via my libraries I extend native objects in different ways to ease
programming. Anyone using my libraries is probably aware of the
drawbacks regarding iteration of those objects and objects based
on those.
PointedEars
Nov 24 '05 #8
Howard Jess wrote:

[...]

Fair enough. I have a code library that corrects some browsers's Javascript
misbehavior, and implements missing features. E.g.

var sel = document.create Element('select ');
var opt = document.create Element('option ');
sel.add(opt,nul l);
I thought the most widely supported method here was:

var sel = document.create Element('select ');
sel.options[sel.options.len gth] = new Option(...);
Maybe that's what you do in your library...


This doesn't work as is on Internet Explorer (which expects either a
single argument, or an index rather than an object reference for the second
argument); the library (with some help) makes it work.

Similarly,

var obj = {thing:1, otherThing:'abc '};
if (obj.hasOwnProp erty('lastThing ')) ...

doesn't work in Safari 1.2: that browser doesn't implement hasOwnProperty,
among others.

It's simple enough to implement it and the rest; e.g.:

Object.prototyp e.hasOwnPropert y = function(propNa me) {
...
}

But in a library, I have no control over user code. And defining it
this way breaks every "for .. in" loop, whose code now must be aware
of the hasOwnProperty in -every- object. *THAT'S* why I'd like to be
able to define a new "DontEnum" property. And yes, I understand it's
not possible in standard JavaScript, and yes, I've begun looking
through the sources; I was hoping someone who frequents this group may
have some insight that might save me some time.
I guess that's a gotcha of JavaScript for..in loops. They've been
discussed here before in relation to sparse arrays, where for..in was
used rather than for..i<array.le ngth and methods had been added to the
Array object or its prototype (e.g. for browsers that don't support
push, splice, etc.).

The answer there was to use a custom object designed for the purpose
(and maybe keeps it's own 'length' property too). Clearly that isn't
what you want.

Thanks for any help or suggestions.


Sorry, none of either...

If it makes you feel any better, it ticks me off that Apple requires the
purchase of an entire OS to get the latest version of their browser,
particularly when it is based on open source. I suppose suggesting
Firefox is of no use? ;-)
--
Rob
Nov 24 '05 #9
VK

RobG wrote:
I suppose suggesting Firefox is of no use? ;-)


Or if the Aqua-style is too precious to you ;-) you may consider Gecko
Camino:
<http://www.caminobrows er.org/>

Safary 1.x - 2.0 is a complete junk as browser. So is the Konqueror
used as the base for it.

Konqueror lesser than 2.2 simply crashes on each site with JavaScript
so you have no chance to make any features check on the first place.

Konqueror 2.2 works 50/50 with JavaScript but there is no common
algorithm to guess where, so there is no use to bother with it.

How this failed programming exercise appeared in both GUI packages (OS
X / KDE) is a great mistery of the new millenium. Definitely some
corporate hash-hash under the carpet we are not aware of.

Any way, Safari 1.x and Konqueror 1.x are not supportable by any
JavaScript checks for the above spelled reasons. Any if-else if -else
's would be a pure waste of time. Just pretend they never existed and
move forward.

Nov 24 '05 #10

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

Similar topics

1
2282
by: Erik Max Francis | last post by:
I've come across a limitation in unpickling certain types of complex data structures which involve instances that override __hash__, and was wondering if it was known (basic searches didn't seem to come up with anything similar) and if there is a workaround for it short of restructuring the data structures in question. The fundamental issue rests with defining classes which override __cmp__ and __hash__ in order to be used as keys in...
0
2314
by: Dotnetified | last post by:
Reposting after about 2 weeks of no response ... thanks if you can help... ---------------------------------------------------------------------------- -------------- To anyone who thinks they know it all: ;) We recently upgraded our MSDN Version of VS.NET 2002 to VS.NET 2003... Things are all working well, except we've run across a new bug or issue since our adopting of the new package.
3
2601
by: Robert Mark Bram | last post by:
Howdy all! When do I call something a property and when do I call something an attribute? It has me a little confused at the moment! Any advice would be most appreciated! Rob
18
4765
by: Robin Becker | last post by:
Is there a way to override a data property in the instance? Do I need to create another class with the property changed? -- Robin Becker
5
6141
by: David N | last post by:
All, I created a control that have the following property defined: private bool autoSearch My control is compiled and created fine. But, after adding the control to my form, I noticed the following problems:
2
3537
by: Harry F. Harrison | last post by:
I get 2 compile errors on assembly attributes after creating a custom attribute. If I comment out the attribute, the errors go away. I don't get it because my attribute specifies class usage, not assembly usage. Assembly attribute 'System.Runtime.InteropServices.GuidAttribute' is not valid: Assembly custom attribute 'System.Runtime.InteropServices.GuidAttribute' was specified multiple times with different values Attribute...
15
1928
by: Sam Kong | last post by:
Hello! I got recently intrigued with JavaScript's prototype-based object-orientation. However, I still don't understand the mechanism clearly. What's the difference between the following two? (1)
2
1682
by: =?Utf-8?B?Z2FkeWE=?= | last post by:
I use one of 2 arrays dependent on the country. Rather than say: if exchangeID = 1 then dim myPlaceBets() as As UK.exchange.PlaceBets many statements myPlaceBetsReq.bets = myPlaceBets else dim myPlaceBets() As AU.exchange.PlaceBets many statements
14
4359
by: Rafe | last post by:
Hi, I've encountered a problem which is making debugging less obvious than it should be. The @property decorator doesn't always raise exceptions. It seems like it is bound to the class but ignored when called. I can see the attribute using dir(self.__class__) on an instance, but when called, python enters __getattr__. If I correct the bug, the attribute calls work as expected and do not call __getattr__. I can't seem to make a simple...
0
10542
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
10289
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
10068
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
9119
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...
1
7600
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6840
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
5496
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...
1
4274
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
3
2968
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.