构造对象的范围 [英] Scope of constructed objects

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

问题描述

只是为了确保我走在正确的轨道上......


函数getADate(){

返回新的Date(); < br $>
}


函数foo(){

var bar = getADate();

alert(吧);

}


这不会像人们预期的那样工作,因为

getADate()构造的日期函数返回后会被销毁,这意味着当调用alert()时,bar

是对被破坏对象的引用。那是

是否正确?


-

Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我

ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。

Just to make sure I''m on the right track...

function getADate() {
return new Date();
}

function foo() {
var bar=getADate();
alert( bar );
}

This doesn''t work as one might expect because the date constructed by
getADate() is destroyed once the function returns, meaning that bar
is a reference to a destroyed object when alert() is called. Is that
correct?

--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.

推荐答案

Christopher Benson-Manica写道:
Christopher Benson-Manica wrote:
只是为了确保我走在正确的轨道上...

函数getADate(){
返回新的Date();


函数foo(){
var bar = getADate();
alert(bar);
}
这不会像人们预期的那样工作,因为
getADate()构造的日期是一旦函数返回就被销毁,这意味着当调用alert()时,bar
是对被破坏对象的引用。那是
正确吗?
Just to make sure I''m on the right track...

function getADate() {
return new Date();
}

function foo() {
var bar=getADate();
alert( bar );
}

This doesn''t work as one might expect because the date constructed by
getADate() is destroyed once the function returns, meaning that bar
is a reference to a destroyed object when alert() is called. Is that
correct?



取决于预期!你的函数返回一个Date对象完成

及其所有方法和属性,初始化为它创建的
的日期和时间。这个对象不会被破坏,直到你的程序被停止

引用它,但也不会(或许令人困惑)时间和日期将自动刷新


Depends what one "expects"! Your function returns a Date object complete
with all its methods and properties, initialised to the date and time it
was created. This object won''t be destroyed until your program''s stopped
referring to it, but nor (perhaps confusingly) will the time and date be
automatically refreshed.


Christopher Benson-Manica说:
Christopher Benson-Manica said:

只是为了确保我走在正确的轨道上......函数getADate(){
返回新的Date();
}
函数foo(){
var bar = getADate();
alert(bar);
}

这不会像人们预期的那样工作,因为一旦函数返回,
getADate()构造的日期就会被破坏,意思是bar
是在调用alert()时对被破坏对象的引用。那是
正确吗?

Just to make sure I''m on the right track...

function getADate() {
return new Date();
}

function foo() {
var bar=getADate();
alert( bar );
}

This doesn''t work as one might expect because the date constructed by
getADate() is destroyed once the function returns, meaning that bar
is a reference to a destroyed object when alert() is called. Is that
correct?




不是。它按预期工作,提醒当前日期和时间。

局部变量是一旦函数返回就会被销毁,但是getBate()中没有局部变量
。 Date对象幸存

,因为仍有对它的引用。一旦foo()返回,

对该Date对象的最后一次引用就会被销毁,并且它将被b $垃圾收集。



No. It works as I expect, alerting the current date and time.
Local variables are destroyed once a function returns, but there
are no local variables in getADate(). The Date object survives
because there is still a reference to it. Once foo() returns,
the last reference to that Date object is destroyed and it will
be subject to garbage collection.


Lee< RE ************** @ cox.net>这样说:
Lee <RE**************@cox.net> spoke thus:
不是。它按照我的预期工作,提醒当前日期和时间。
一旦函数返回,局部变量就会被破坏,但是
是getADate()中没有局部变量。由于仍有对它的引用,Date对象仍然存在。一旦foo()返回,
对该Date对象的最后一个引用将被销毁,它将被收集垃圾。
No. It works as I expect, alerting the current date and time.
Local variables are destroyed once a function returns, but there
are no local variables in getADate(). The Date object survives
because there is still a reference to it. Once foo() returns,
the last reference to that Date object is destroyed and it will
be subject to garbage collection.




那么为什么那么,当该窗口被销毁时,对另一个窗口中的变量的引用会变成

无效吗?


-

Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我

ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。



So why, then, does a reference to a variable in another window become
invalid when that window is destroyed?

--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.


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

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