访问对象名称 [英] Accessing an object name

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

问题描述

嘿所有,出于调试目的,我希望能够从对象类中访问

对象的实例名称。例如,我希望
能够做到这样的事情:


------------- -----------------------------------------------

函数FooClass(){

// FooClass成员的定义

}


FooClass.prototype.Debug = function(){

//这里需要帮助以显示正确的信息

alert(我的名字是......?);

}


var monkeybutter = new FooClass();

monkeybutter.Debug(); //我想要这样显示我的名字是

monkeybutter

----------------- -------------------------------------------

这是可能的,如果是的话,怎么样?

解决方案

11月26日下午5:57,TonyV< kingskip。 .. @ gmail.comwrote:


嘿所有,为了调试目的,我希望能够访问

对象来自对象类的实例名称。例如,我希望
能够做到这样的事情:


------------- -----------------------------------------------

函数FooClass(){

// FooClass成员的定义


}


FooClass .prototype.Debug = function(){

//这里需要帮助来显示正确的信息

alert(我的名字是......?);


}


var monkeybutter = new FooClass();

monkeybutter.Debug(); //我想要这样显示我的名字是

monkeybutter

----------------- -------------------------------------------

这可能,如果可以,怎么样?



arguments.callee construct可能会有所帮助。
http://www.devguru.com/Technologies/...arguments.html

Kailash Nadh | http://kailashnadh.name


11月26日晚上8:57,TonyV< kingskip ... @ gmail.comwrote:


嘿所有,为了调试目的,我想能够从对象类中访问

对象的实例名称。例如,我希望
能够做到这样的事情:


------------- -----------------------------------------------

函数FooClass(){

// FooClass成员的定义


}


FooClass .prototype.Debug = function(){

//这里需要帮助来显示正确的信息

alert(我的名字是......?);


}


var monkeybutter = new FooClass();

monkeybutter.Debug(); //我希望这个显示我的名字是

monkeybutter



var monkeybutter = new FooClass(''monkeybutter'');

function FooClass(){

this._name = arguments [0] || ''anonymous'';

// FooClass成员的定义

}


FooClass.prototype.Debug = function() {

alert(我的名字是+ this._name);

}


< blockquote> On Mon,2007年11月26日09:57:29,在comp.lang.javascript中,TonyV写道:


>嘿所有,为了调试目的,我希望能够从对象类中访问
对象的实例名称。例如,我希望能够做到这样的事情:

--------------------- ---------------------------------------
function FooClass(){

// FooClass成员的定义
}

FooClass.prototype.Debug = function(){

//需要帮助来显示正确的信息

alert(我的名字是......?);


var monkeybutter = new FooClass();



var a = monkeybutter;

var b = a;

var c = b;


> monkeybutter.Debug(); //我希望这个显示我的名字是
猴子



现在你要显示什么:a,b,c或monkeybutter?

(注意:所有这些变量都指向同一个对象)。


如果你也做了什么


def [" fred" ;]()。gh = c;


现在你要显示什么?


> --- -------------------------------------------------- -------

这是可能的,如果是的话,怎么样?



经过一番思考后,你现在意识到这是错误的问题。

像往常一样,你需要从正确开始问题:


我真的想要做什么?


从你的第一段看起来好像你想要的每个FooClass

对象保存自己的对象标识符。然后VK'的解决方案是你的回答,虽然称之为''oid'',而不是''name'',因为''name''很可能是
$实名需要b $ b。现在oid可以是方便的或者b
重要,不限于全局变量的名称。


John

- -

John Harris


Hey all, for debugging purposes, I''d like to be able to access an
object''s instance name from within the object class. For example, I''d
like to be able to do something like this:

------------------------------------------------------------
function FooClass() {
// definition of FooClass members
}

FooClass.prototype.Debug = function() {
// Need help here to display correct information
alert("My name is ...?");
}

var monkeybutter = new FooClass();
monkeybutter.Debug(); // I''d like this to display "My name is
monkeybutter"
------------------------------------------------------------

Is this possible, and if so, how?

解决方案

On Nov 26, 5:57 pm, TonyV <kingskip...@gmail.comwrote:

Hey all, for debugging purposes, I''d like to be able to access an
object''s instance name from within the object class. For example, I''d
like to be able to do something like this:

------------------------------------------------------------
function FooClass() {
// definition of FooClass members

}

FooClass.prototype.Debug = function() {
// Need help here to display correct information
alert("My name is ...?");

}

var monkeybutter = new FooClass();
monkeybutter.Debug(); // I''d like this to display "My name is
monkeybutter"
------------------------------------------------------------

Is this possible, and if so, how?

arguments.callee construct might be of help.
http://www.devguru.com/Technologies/...arguments.html

Kailash Nadh | http://kailashnadh.name


On Nov 26, 8:57 pm, TonyV <kingskip...@gmail.comwrote:

Hey all, for debugging purposes, I''d like to be able to access an
object''s instance name from within the object class. For example, I''d
like to be able to do something like this:

------------------------------------------------------------
function FooClass() {
// definition of FooClass members

}

FooClass.prototype.Debug = function() {
// Need help here to display correct information
alert("My name is ...?");

}

var monkeybutter = new FooClass();
monkeybutter.Debug(); // I''d like this to display "My name is
monkeybutter"

var monkeybutter = new FooClass(''monkeybutter'');
function FooClass() {
this._name = arguments[0] || ''anonymous'';
// definition of FooClass members
}

FooClass.prototype.Debug = function() {
alert("My name is " + this._name);
}


On Mon, 26 Nov 2007 at 09:57:29, in comp.lang.javascript, TonyV wrote:

>Hey all, for debugging purposes, I''d like to be able to access an
object''s instance name from within the object class. For example, I''d
like to be able to do something like this:

------------------------------------------------------------
function FooClass() {
// definition of FooClass members
}

FooClass.prototype.Debug = function() {
// Need help here to display correct information
alert("My name is ...?");
}

var monkeybutter = new FooClass();

var a = monkeybutter;
var b = a;
var c = b;

>monkeybutter.Debug(); // I''d like this to display "My name is
monkeybutter"

Now what do you want to display : "a", "b", "c", or "monkeybutter"?
(Note : all those variables point to the same object).

What if you also did

d.e.f["fred"]().g.h = c;

Now what do you want to display?

>------------------------------------------------------------

Is this possible, and if so, how?

After a bit of thought you now realise that that was the wrong question.
As usual, you need to start with the right first question :

"What do I *really* want to do?"

From your first paragraph it looks as though you want each FooClass
object to hold its own object identifier. Then VK''s solution is your
answer, though call it ''oid'', not ''name'', as ''name'' is too likely to be
needed for real names. Now the oid can be something convenient or
significant, not restricted to the name of a global variable.

John
--
John Harris


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

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