另一个对象字面问题! [英] Another Object literal Question!

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

问题描述

下面我用2种方法声明了一个基本的对象文字。

" doSomething"方法是从useDoSomething调用的。方法,但是如果我使用this,那么

通话才会成功。关键字或限定电话

与SomeObj。


有人可以描述为什么会这样吗?


var SomeObj = {

doSomething:function()

{

return''做了什么';

},


useDoSomething:function()

{

//不行 - doSomething()没有定义。

返回doSomething();

//作品

//返回this.doSomething();

//作品

//返回SomeObj.doSomething();

}

}


alert(SomeObj.doSomething());

alert(SomeObj.useDoSomething());

Below I declared a basic object literal with 2 methods. The
"doSomething" method is call from the "useDoSomething" method but the
call is only sucessful if I use the "this" keyword or qualify the call
with "SomeObj".

Can someone describe why this is happening?

var SomeObj = {
doSomething : function()
{
return ''Did something'';
},

useDoSomething : function()
{
//Doesn''t work - doSomething() no defined.
return doSomething();
//Works
//return this.doSomething();
//Works
//return SomeObj.doSomething();
}
}

alert(SomeObj.doSomething());
alert(SomeObj.useDoSomething());

推荐答案


em*********@gmail.com 写道:

下面我用2个方法声明了一个基本对象文字。

" doSomething"方法是从useDoSomething调用的。方法,但是如果我使用this,那么

通话才会成功。关键字或限定电话

与SomeObj。


有人可以描述为什么会这样吗?


var SomeObj = {

doSomething:function()

{

return''做了什么';

},


useDoSomething:function()

{

//不行 - doSomething()没有定义。

返回doSomething();

//作品

//返回this.doSomething();

//作品

//返回SomeObj.doSomething();

}

}


alert(SomeObj.doSomething());

alert(SomeObj.useDoSomething());
Below I declared a basic object literal with 2 methods. The
"doSomething" method is call from the "useDoSomething" method but the
call is only sucessful if I use the "this" keyword or qualify the call
with "SomeObj".

Can someone describe why this is happening?

var SomeObj = {
doSomething : function()
{
return ''Did something'';
},

useDoSomething : function()
{
//Doesn''t work - doSomething() no defined.
return doSomething();
//Works
//return this.doSomething();
//Works
//return SomeObj.doSomething();
}
}

alert(SomeObj.doSomething());
alert(SomeObj.useDoSomething());



此主题可能有所帮助:


< URL:
http://groups.google.com/group/comp....a7ee1707cd0a9a


>
>



-

Rob

--
Rob




RobG写道:

[...]

RobG wrote:
[...]

此主题可能有所帮助:
This thread may help:



实际上更好的参考是理查德的封闭文章,它非常全面。它涵盖的不仅仅是闭包:


< URL: http://www.jibbering.com/faq/faq_notes/closures.html >

-

Rob

Actually a better reference is Richard''s closures article, it is very
comprehensive. It covers much more than just closures:

<URL: http://www.jibbering.com/faq/faq_notes/closures.html >
--
Rob


我还是有点困惑,因为我看不到任何关闭的地方

形成。


我不确定为什么对象文字的一个方法没有

访问同一个对象文字的另一个方法。

我可能不会很好地解释自己,对不起!

RobG写道:
I''m still a little confused because I don''t see where any closures are
formed.

I am unsure of why an one method of an object literal does not have
access to another method of the same object literal.

I''m probably not explain myself very well, sorry!
RobG wrote:

RobG写道:

[...]
RobG wrote:
[...]

此主题可能会有所帮助:
This thread may help:



其实a更好的参考是理查德的封闭文章,它非常全面。它涵盖的不仅仅是闭包:


< URL: http://www.jibbering.com/faq/faq_notes/closures.html >


-

Rob


Actually a better reference is Richard''s closures article, it is very
comprehensive. It covers much more than just closures:

<URL: http://www.jibbering.com/faq/faq_notes/closures.html >
--
Rob


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

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