JavaScript中的反思 [英] Reflection in JavaScript

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

问题描述




我正在处理一个对象,它有一个调用setInterval的函数,所以

它可以有一个重复调用的函数。现在,要调用

函数,你还必须提供实例的名称

调用它(即.oncement.start(''notify'' ,500))。有没有办法让

实例读取自己的名称并将其作为字符串提供,以便启动函数所需的

唯一参数是它的间隔

应该开火吗?另外,有没有办法让对象保持一个函数

私有但仍然有setInterval调用它?


提前谢谢


Chris Lieb

Hi,

I am working on an object that has a function that calls setInterval so
that it can have a function called repeatedly. Right now, to call the
function, you also have to provide the name of the instance that is
calling it (ie. notify.start(''notify'', 500)). Is there a way to have
the instance read its own name and provide it as a string so that the
only parameter required for the start function is the interval that it
should fire at? Also, is there a way for the object to keep a function
private but still have setInterval call it?

Thanks in advance

Chris Lieb

推荐答案



Chris Lieb写道:

Chris Lieb wrote:
我正在处理一个对象,它有一个调用setInterval的函数,所以它可以有一个重复调用的函数。现在,要调用
函数,您还必须提供调用它的实例的名称(即notify.start(''notify'',500))。有没有办法让实例读取自己的名称并将其作为字符串提供,以便启动函数所需的唯一参数是它应该触发的间隔?


此问题和可能的解决方案已经在这里进行了探讨:

< http://groups.google.com/group/comp.lang.javascript/ browse_frm / thread / 3f544326c39ca6d9 / 49f961e7e57922f7>


不幸的是,这个 JavaScript中的实现是borken(至少从外部角度看是
)并且解释器没有将

上下文切换回内部对象方法的对象

情况。

另外,有没有办法让对象保持一个函数私有但仍然有setInterval调用它?
I am working on an object that has a function that calls setInterval so
that it can have a function called repeatedly. Right now, to call the
function, you also have to provide the name of the instance that is
calling it (ie. notify.start(''notify'', 500)). Is there a way to have
the instance read its own name and provide it as a string so that the
only parameter required for the start function is the interval that it
should fire at?
This question and possible solutions have been explored here:
<http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/3f544326c39ca6d9/49f961e7e57922f7>

Unfortunately yes, "this" implementation is borken in JavaScript (at
least from a outside point of view) and interpreter doesn''t switch the
context back to the object for internal object methods in this
situation.
Also, is there a way for the object to keep a function
private but still have setInterval call it?




似乎私有和受保护的范围不能在

标准JavaScript中模拟。只要给你的受保护即方法和领域一些

criptic名称如



Seems like private and protected scopes are not emulable in the
standard JavaScript. Just give your "protected" methods and fields some
criptic names like


_functionOne并希望最好。 : - (

_functionOne and hope to the best. :-(


2005年11月21日23:12,VK写道:


[snip]
On 21/11/2005 23:12, VK wrote:

[snip]
是否有办法让实例读取自己的名称并将其作为字符串提供,以便该实例所需的唯一参数开始
函数是它应该触发的间隔?


不,因为几个标识符可能引用同一个对象,使得

的概念一个名字荒谬。


[snip]
Is there a way to have the instance read its own name and provide
it as a string so that the only parameter required for the start
function is the interval that it should fire at?
No, because several identifiers may reference the same object, making
the notion of a single name nonsensical.

[snip]
不幸的是,这个实现在JavaScript中是borken
[。 ..]


不,它不是。这个操作符的行为是明确定义的。

并不是说其他行为有时候也是需要的,但是这不是和b $ b一样的损坏。


[snip]

此外,有没有办法让对象保持一个私有的功能,但
仍然有setInterv怎么称呼?
Unfortunately yes, "this" implementation is borken in JavaScript
[...]
No, it isn''t. The behaviour of the this operator is well-defined. That
isn''t to say that other behaviour is sometimes wanted, but that''s not
the same as broken.

[snip]
Also, is there a way for the object to keep a function private but
still have setInterval call it?




是和否。


setTimeout和setInterval方法可以接受函数引用,所以

可以传递对私有的函数对象的引用:


函数MyObject(){

var self = this,

timer;


函数回调(){

/ *使用self来引用MyObject的实例。 * /

}


this.start = function(句点){

timer = setInterval(callback,period); < br $>
};

}


但是,有些浏览器只会期望一个字符串参数并强制兑换

函数为一个字符串(有效地调用它的toString方法),在全局范围内评估它是否为
。这可以相对容易地处理,但是它会意味着公开私有函数以便可以访问全局(尽管有些隐蔽):


函数MyObject(){

var self = this,

timer;


函数回调( ){

/ *使用self来引用MyObject的实例。 * /

}

callback.toString = function(){

return''MyObject ["



Yes and no.

The setTimeout and setInterval methods can take function references, so
it is possible to pass a reference to a function object that is private:

function MyObject() {
var self = this,
timer;

function callback() {
/* Use self to refer to instance of MyObject. */
}

this.start = function(period) {
timer = setInterval(callback, period);
};
}

However, some browsers only expect a string argument and will coerce the
function to a string (effectively call its toString method), evaluating
it in global scope. This can be dealt with relatively easily, but it
would mean exposing the private function so that it could be accessed
globally (though somewhat covertly):

function MyObject() {
var self = this,
timer;

function callback() {
/* Use self to refer to instance of MyObject. */
}
callback.toString = function() {
return ''MyObject["


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

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