Javascript 到达一个物体';s属性

Javascript 到达一个物体';s属性,javascript,timer,setinterval,Javascript,Timer,Setinterval,在我的应用程序中,我有一个具有多个属性的对象,这些属性在应用程序的不同位置设置 在我的一个原型函数中,有一个函数每隔一段时间运行以更新计时器,在该函数中,应该设置属性(this.)time。问题是这不会发生,我想原因是this。时间指向函数本身,而不是对象 下面是我的代码的两个版本,它们都不起作用。有给我的建议吗 // 1. function changeTime() { this.theTime = setTime(time); time.setSeconds(time.get

在我的应用程序中,我有一个具有多个属性的对象,这些属性在应用程序的不同位置设置

在我的一个原型函数中,有一个函数每隔一段时间运行以更新计时器,在该函数中,应该设置属性(this.)time。问题是这不会发生,我想原因是
this。时间
指向函数本身,而不是对象

下面是我的代码的两个版本,它们都不起作用。有给我的建议吗

// 1.
function changeTime() {
    this.theTime = setTime(time);
    time.setSeconds(time.getSeconds()+1);
    p1.html(this.theTime);
}
interval = setInterval(changeTime(), 1000 );

// 2.
function changeTime(theTime) {
    theTime = setTime(time);
    time.setSeconds(time.getSeconds()+1);
    p1.html(theTime);
}
interval = setInterval( function() { changeTime(this.theTime); }, 1000 );

更清楚地说,上面的函数每秒更新一个计时器(例如,
00:00:01
->
00:00:02
),我希望
这个。时间将随着时间而更新

当计时器停止时(发生在另一个原型函数中),我希望能够看到计时器停止的时间,但现在是
这个。time
是默认值,这意味着上面的函数不更新objects属性。而不是
此项。上述函数中的time
必须是局部变量


注意:
setTime()
是与上述函数存在于同一原型函数中的另一个函数。

您可以通过编写

debugger;

在函数中设置断点。然后很容易找到你的问题。

你的假设是正确的,你的
这个
关键字有问题这个
有点棘手,因此在函数中使用它(尤其是
setTimeout
setInterval
是有风险的

您要做的是在创建函数时保存
this
的值


这里有更多信息:

也许这些评论会指引你正确的方向

var theTime; // global variable
function changeTime() {
    theTime = setTime(time); // theTime is global variable declared above (accesible from anywhere)
    // var myTime = setTime(time); // myTime is local variable
    time.setSeconds(time.getSeconds()+1);
    p1.html(theTime);
}
interval = setInterval(changeTime, 1000 ); // no braces

当你在某个函数中使用它时,
这个
引用的是实际函数所在的对象。这里:

function myF() {
    this.var = 'hey';
}
使用此函数(myF作为构造函数),可以访问
var

或在此:

function myF2() {
    if (typeof this.var === 'undefined') {
        this.var = 0;
    } else {
        this.var += 1;
    }
    alert(this.var);
}
这里的
var
也是myF2的一个属性(正如我所说的,它不仅仅是一个函数,因为在JavaScript中函数是对象)。 每次调用myF2时,this.var都会递增并发出警报(就在第一次调用时,它会被初始化)

在第二个函数(第二个setInterval中使用的匿名函数)中,您也在执行相同的操作

一种解决方案是在这两种情况下都将时间设置为全局时间,这样您就不需要使用:

this.theTime
所以结果可能是这样的:

var theTime = 0, interval;
function changeTime() {
    theTime += 1;
    document.body.innerHTML = theTime;
    setInterval
}

interval = setInterval(changeTime, 1000 );

Jason,在你澄清之后,我认为最好给你提供一个全新的答案,试图用JS尽可能好(和简单)地解释这句话。我希望它能有所帮助

<html>
<body>
<div id="output1"></div>
<div id="output2"></div>
<script>
// theTime is undefined in global scope

function obj(target) {
  var theTime = 0; 
  var that = this; // var means "private"
  this.changeTime = function() { // here "this" points to obj and means "public"
    theTime++; // no var => outer scope = obj scope
    // here "this" points to changeTime function, not to obj!
    // "that" points to obj, you may use that.theTime
    document.getElementById(target).innerHTML = theTime;
  }
}

var o1 = new obj("output1");
var o2 = new obj("output2");

setInterval(o1.changeTime,1000); // update output1 content every second
setInterval(o2.changeTime,500); // update output2 content twice a second
</script>
</body>
</html>

//时间在全局范围内未定义
功能obj(目标){
var theTime=0;
var that=this;//var表示“私有”
this.changeTime=function(){//此处“this”指向obj,表示“public”
time++;//无var=>outer scope=obj scope
//这里的“this”指向changeTime函数,而不是obj!
//“that”指的是obj,你可以用它
document.getElementById(target.innerHTML=时间;
}
}
var o1=新的obj(“输出1”);
var o2=新的obj(“输出2”);
setInterval(o1.changeTime,1000);//每秒更新output1内容
setInterval(o2.changeTime,500);//每秒更新两次output2内容

应该设置为什么?您的代码无法工作,但不知道如何准确地修复它。此
值是在每次函数调用时设置的;在任何函数中它都不是永久固定的。是
设置时间()
您编写的函数,或者您正在尝试使用JS函数
Date.setTime()
?@JasonCraig我为您编辑了更多的问题,以清理更多的东西。他们喜欢这里的鱼,而不是钓鱼杆小心,如果
time.getSeconds()
60
,那么您就要
time.setSeconds(61)
@Imre L:…很好,谢谢你的回答。但是,我不明白?在你的代码中,你在函数中声明了变量“theTime”。我应该如何从另一个(原型)中获得它当我没有从它调用任何函数时使用函数?@Jason Craig:那么你应该在全局范围内声明
var theTime;
,并在
changeTime
函数中使用
theTime=setTime(time)
(不带
var
)。在每个声明中使用
var
关键字以保护您的代码不被覆盖全局变量。但如果我在全局范围中将变量声明为“var”而不是“this”,它将不会指向特定实例?注释过于简单,请宽容,JS极客。
<html>
<body>
<div id="output1"></div>
<div id="output2"></div>
<script>
// theTime is undefined in global scope

function obj(target) {
  var theTime = 0; 
  var that = this; // var means "private"
  this.changeTime = function() { // here "this" points to obj and means "public"
    theTime++; // no var => outer scope = obj scope
    // here "this" points to changeTime function, not to obj!
    // "that" points to obj, you may use that.theTime
    document.getElementById(target).innerHTML = theTime;
  }
}

var o1 = new obj("output1");
var o2 = new obj("output2");

setInterval(o1.changeTime,1000); // update output1 content every second
setInterval(o2.changeTime,500); // update output2 content twice a second
</script>
</body>
</html>