object's name (anti-eval)

  • Thread starter Oleg Alistratov
  • Start date
O

Oleg Alistratov

Is in javascript a method to get object's identifier as string?
E.g., may I write function GetName thus, that

var v;
var s = GetName(v);

has led to

s == "v"

Please, help.
 
L

Lee

Oleg Alistratov said:
Is in javascript a method to get object's identifier as string?
E.g., may I write function GetName thus, that

var v;
var s = GetName(v);

has led to

s == "v"

No. It would make no sense, since the same object can have many
different names. Also, if you think you need an object's name,
you are probably making a design mistake of some sort.
 
D

Douglas Crockford

Is in javascript a method to get object's identifier as string?
E.g., may I write function GetName thus, that

var v;
var s = GetName(v);

has led to

s == "v"

You are correct in wanting to avoid eval. Generally, an object's name is
irrelevant because you are working with object references, not object
names. If you need object names in order to avoid learning to use object
references, then you should obtain a better book. See
http://www.crockford.com/javascript/javascript.html

Names can be useful in some applications. In those cases, keep the
objects in a little database.

var database = {};
function MyObject(name) {
this.name = name;
database[name] = this;
}

So,

object.name

returns an object's name.

database[name]

returns the object.
 
O

Oleg Alistratov

Lee said:
No. It would make no sense, since the same object can have many
different names. Also, if you think you need an object's name,
you are probably making a design mistake of some sort.

Thank you!

I'll try to describe the problem as a whole. I hope, somebody may offer
the best decision.

The class Button holds properties which describes a graphic button.
Its method Button.Draw write the corresponding content:

function Button_Draw()
{
document.write(
"<div id=\"" + this.ID + "\"" +
" onclick=\"Button_Click();\">"
);
...

Several buttons are grouping in the button bar:
Bar's constructor:

function Bar(buttons)
{
this.Items = new Array(arguments.length);
for (i = 0; i < arguments.length; i++)
this.Items = arguments;
}

In event handler Button_Click I need to get properties of Button object,
which was creating this DIV element.

If we have name of the Bar object, we may build the Button_Click calling
in that way:

"onclick=\"Button_Click('" + BarName + "', " + ButtonIndex + ");\""

This is allow using in Button_Click the following code:

function ButtonClick(BarName, ButtonIdx)
{
var t = eval(BarName).Items[ButtonIdx].Caption;
....
}

For this case I need to get object's name as string variable (BarName).

I understand that this code looks ugly, but I do not see a way to make
it better.

Please, help!
 
M

Matt Kruse

Oleg said:
The class Button holds properties which describes a graphic button.
Its method Button.Draw write the corresponding content:
... [and written-out content needs to refer back to its writer] ...

What I do in some of my libs at the url below is to have a global array that
stores references to objects of a certain type. Each time an object is
created, it gets a unique index as one of its properties, and adds a
reference to itself into the global array.

So, any time it needs to create a stand-alone reference to itself, it can do
something like
document.write("GlboalObjectArray[" + this.index + "].doSomething()");

There may be better ways to do it, but this works well for me.
 
R

Randy Webb

Oleg Alistratov wrote:

function ButtonClick(BarName, ButtonIdx)
{
var t = eval(BarName).Items[ButtonIdx].Caption;

var t=window[BarName].......

All Global variables are properties of the window object
 
O

Oleg Alistratov

Duncan said:
Now all you have to do in Button_Click is use the ID to match the event to
the button:

function Button_Click(theDiv) {

var button = thebar.ButtonsById[theDiv.ID]; ......................======
...

}

I could found independently to such decision. The further experiments
have appeared, because it was necessary to use multiple bars in the same
document:

var button = SomethingBig.BarById[theBar.ID].ButtonsById[theDiv.ID];

I assumed to use

eval(BarName)

instead

SomethingBig.BarById[theBar.ID]

This situation has led to a problem described in the my first message.
BTW, you would be much better to manipulate the HTML DOM directly instead
of using document.write.

Thanks, it's a good idea. This approach is new to me, I'll try to
understand more in detail. Thank you!
 
L

lawrence

Douglas Crockford said:
Names can be useful in some applications. In those cases, keep the
objects in a little database.

var database = {};
function MyObject(name) {
this.name = name;
database[name] = this;
}

So,

object.name

returns an object's name.

database[name]

returns the object.

Pardon the newbie question, but what does the word "this" mean here?
It seems to have a different meaning in Java and PHP.
 
L

lawrence

Duncan Booth said:
BTW, you would be much better to manipulate the HTML DOM directly instead
of using document.write. That would let you associate a function directly
with the event instead of having to use a text string. A function can
access variables in an outer scope when it was defined, so you can bypass
all of the id lookups and do something like:

function AssociateButtonWithDiv(div, button) {
function clicked() {
button.clicked(div);
}

if (isMozilla) {
div.addEventListener("click", clicked, false);
} else if (isIE) {
div.attachEvent("onclick", clicked);
}
}

All code samples above are untested.

Forgive the ignorant question, but what is isMozilla? Is that a
constant that exists in Mozilla, to let you know that you're dealing
with Mozilla? Where did you find it? Can you point me to some
documentation regarding it?
 
R

Randy Webb

lawrence said:
Forgive the ignorant question, but what is isMozilla? Is that a
constant that exists in Mozilla, to let you know that you're dealing
with Mozilla? Where did you find it? Can you point me to some
documentation regarding it?

It falls in the same place the isIE variable falls. Its an utterly
flawed attempt to detect the browser when the browser doesn't matter.
The only way that isMozilla/isIE would be accurate in that scenario is
if its defined like this:

if(div.addEventListener) isMozilla = true;

All of it is more predictable when written something like this:

if (div.addEventListener) {
div.addEventListener("click", clicked, false);
} else if (div.attachEvent) {
div.attachEvent("onclick", clicked);
}

Even then, div might not be a good variable name.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top