在OO Javascript中使用计时器 [英] Using timers within OO Javascript

查看:58
本文介绍了在OO Javascript中使用计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿所有,我在一个

自定义对象/原型中遇到了window.setInterval()的问题。这是我的代码:

Hey all, I''m having some trouble with window.setInterval() within a
custom object/prototype. Here is my code:

展开 | 选择 | Wrap | 行号

推荐答案

Evan Charlton写道:
Evan Charlton wrote:

嘿所有,我在一个

自定义对象/原型中遇到了window.setInterval()的问题。这是我的代码:


[代码]

函数MyClass(){
Hey all, I''m having some trouble with window.setInterval() within a
custom object/prototype. Here is my code:

[code]
function MyClass() {



为什么在谈到OOP时,人们如此着迷于课堂?从上面的

我认为你已经明白ECMAScript 3实现只使用

基于原型的继承。

Why are people so obsessed with classes when it comes to OOP? From the
above I thought you had understood that ECMAScript 3 implementations use
only prototype-based inheritance.


//做一些垃圾

// ...

//定义方法

this.m_one = function(){

//此行没有任何反应。没有警报,没有错误

window.setInterval(" this.m_two",500);
// do some junk
// ...
// define methods
this.m_one = function() {
// nothing happens with this line. No alert, no errors
window.setInterval( "this.m_two", 500 );



为什么要这样?它仅仅是对全局对象*的

不存在的属性m_two*的评估读取访问权限。

Why should there? It is merely a not evaluated read access to the
non-existing property `m_two'' *of the Global Object*.


/ /错误:this.m_two()不是函数

window.setInterval(" this.m_two()",500);
// "error: this.m_two() is not a function"
window.setInterval( "this.m_two()", 500 );



这是试图调用全局对象的方法。由于无法解析

标识符,因此会出现运行时错误。

This is trying to call a method of the Global Object. Since the
identifier cannot be resolved, you get the runtime error.


//" error:m_two()未定义

window.setInterval(" m_two()",500);
// "error: m_two() is not defined"
window.setInterval( "m_two()", 500 );



这是试图在没有显式对象的情况下调用标识符

引用。由于无法通过范围

链解析标识符,因此会出现运行时错误。

This is trying to call an identifier without an explicit object
reference. Since the identifier cannot be resolved through the scope
chain, you get the runtime error.


//"错误:m_two不是定义

window.setInterval(" m_two",500);
// "error: m_two is not defined"
window.setInterval( "m_two", 500 );



这是试图访问没有明确对象的标识符

引用。由于无法通过范围

链解析标识符,因此会出现运行时错误。


如您所见,您不能在本地定义的名称中使用一个字符串,用于

window.setInterval()方法(它与

window.setTimeout())相同。但是,您可以传递一个Function对象

引用,这是对您的方法的引用:


window.setInterval(this.m_two,500);

This is trying to access an identifier without an explicit object
reference. Since the identifier cannot be resolved through the scope
chain, you get the runtime error.

As you can see, you cannot use a locally defined name in a string for
the window.setInterval() method (it would be the same with
window.setTimeout()). However, you can pass a Function object
reference, that is a reference to your method:

window.setInterval(this.m_two, 500);


//以下一个工作正常。

window.setInterval(" timer()",500);
// the following one works just fine .
window.setInterval( "timer()", 500 );



因为timer()是全局声明的函数,并且(所以)全局对象的方法是
。 " this.timer()"也应该工作。


}

Because timer() is a globally declared function, and (so) a method of
the Global Object. "this.timer()" should work as well.

}


this.m_two = function(){

提醒(m_two);

}

}

[...]
this.m_two = function() {
alert("m_two");
}
}
[...]


HTH


PointedEars

-

var bugRiddenCrashPronePieceOfJunk =(

navigator .userAgent.indexOf(''MSIE 5'')!= -1

&& navigator.userAgent.indexOf(''Mac'')!= -1

)// Plone,register_function.js:16


HTH

PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf(''MSIE 5'') != -1
&& navigator.userAgent.indexOf(''Mac'') != -1
) // Plone, register_function.js:16


Thomas''PointedEars''Lahn写道:
Thomas ''PointedEars'' Lahn wrote:

如您所见,您不能在字符串中使用本地定义的名称

window.setInterval()方法(它与

窗口相同。的setTimeout())。但是,你可以传递一个Function对象

引用,这是对你的方法的引用:


window.setInterval(this.m_two,500);
As you can see, you cannot use a locally defined name in a string for
the window.setInterval() method (it would be the same with
window.setTimeout()). However, you can pass a Function object
reference, that is a reference to your method:

window.setInterval(this.m_two, 500);



更正,使用它来保留方法 - 对象关系:


window.setInterval(function callTwo(){ this.m_two();},500);

PointedEars

-

var bugRiddenCrashPronePieceOfJunk =(

navigator.userAgent.indexOf(''MSIE 5'')!= -1

&& navigator.userAgent.indexOf(''Mac'')!= -1

)// Plone,register_function.js:16

Correction, use this instead to retain the method-object relationship:

window.setInterval(function callTwo() { this.m_two(); }, 500);
PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf(''MSIE 5'') != -1
&& navigator.userAgent.indexOf(''Mac'') != -1
) // Plone, register_function.js:16


Thomas''PointedEars''Lahn写道:
Thomas ''PointedEars'' Lahn wrote:

Thomas''PointedEars''Lahn写道:
Thomas ''PointedEars'' Lahn wrote:

>如你所见,你不能在
字符串中使用本地定义的名称window.setInterval()方法(它与window.setTimeout()相同)。但是,你可以传递一个Function对象引用,这是对你的方法的引用:

window.setInterval(this.m_two,500);
>As you can see, you cannot use a locally defined name in a
string for the window.setInterval() method (it would be
the same with window.setTimeout()). However, you can
pass a Function object reference, that is a reference to
your method:

window.setInterval(this.m_two, 500);



更正,使用它来保留方法 - 对象关系:


window.setInterval(function callTwo(){ this.m_two();},500);


Correction, use this instead to retain the method-object relationship:

window.setInterval(function callTwo() { this.m_two(); }, 500);



这不会有帮助,因为当setInterval执行函数

表达式(或者函数评估产生的对象时) />
表达式,确切地说 - 这个 - 函数内部的引用

将是全局对象所以 - this.m_two - 将具有相同的

意思是在前面例子中的字符串中。


Richard。

That is not going to help because when setInterval executes the function
expression (or the object resulting from the evaluation of the function
expression, to be precise) the - this - reference inside that function
will be to the global object and so - this.m_two - will have the same
meaning as when it was in the string in the earlier examples.

Richard.


这篇关于在OO Javascript中使用计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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