对象的名称(反eval) [英] object's name (anti-eval)

查看:62
本文介绍了对象的名称(反eval)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在javascript中是一个将对象'的标识符作为字符串的方法吗?

例如,我可以写函数GetName,那个


var v ;

var s = GetName(v);


导致


s ==" v"


请帮忙。

解决方案

Oleg Alistratov说:


在javascript中是一个将对象的标识符作为字符串的方法吗?
例如,我可以编写函数GetName,那个

var v;
var s = GetName(v);

导致

s ==" v"




No.没有意义,因为同一个对象可以有很多不同的名字。另外,如果你认为你需要一个对象的名字,

你可能会犯某种设计错误。


< BLOCKQUOTE>>在javascript中是一个将对象的标识符作为字符串获取的方法吗?

例如,我可以编写函数GetName,那个

var v;
var s = GetName(v);

导致

s ==" v"




你是想要避免评估是正确的。通常,对象的名称是

不相关,因为您正在使用对象引用,而不是对象

名称。如果你需要对象名称以避免学习使用对象

引用,那么你应该获得一本更好的书。请参阅
http://www.crockford.com/javascript/javascript .html


名称在某些应用程序中很有用。在这些情况下,将

对象保存在一个小数据库中。


var database = {};

函数MyObject(名称) ){

this.name = name;

database [name] = this;

}


所以,


object.name


返回一个对象的名称。


数据库[名称]


返回对象。


Lee写道:

不会没有意义,因为同一个对象可以有很多不同的名字。另外,如果你认为你需要一个对象的名字,
你可能会犯某种设计错误。




谢谢!


我会尝试将问题描述为一个整体。我希望,有人可能会提供

最佳决定。


类Button包含描述图形按钮的属性。

方法Button.Draw写相应的内容:


函数Button_Draw()

{

document.write(

"< div id = \"" + this.ID +" \"" +

" onclick = \" Button_Click() ; \">"

);

...


按钮栏中有几个按钮分组:

Bar'的构造函数:


函数栏(按钮)

{

this .Items = new Array(arguments.length);

for(i = 0; i< arguments.length; i ++)

this.Items [i] = arguments [i];

}


在事件处理程序中Button_Click我需要获取Button对象的属性,

这是创建这个DIV元素。


如果我们有Bar对象的名字,我们可以构建Button_Click以这种方式调用




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


这是允许在Button_Click中使用以下代码:


函数ButtonClick( BarName,ButtonIdx)

{

var t = eval(BarName).Items [ButtonIdx] .Caption;

....

}


对于这种情况,我需要将对象的名称作为字符串变量(BarName)。


我知道这段代码看起来很难看,但是我没有办法让

更好。


请帮忙!


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.

解决方案

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.


> 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.


Lee wrote:

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[i] = arguments[i];
}

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!


这篇关于对象的名称(反eval)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆