为何下面会是相等的

wtcsy 2010-02-09 11:00:02
var  vvv = "1";
function cc(){
var vv = "1";
function xx(){

}
alert(xx.__parent__);
alert(xx.__parent__ === cc.__parent__);
}
alert(cc.__parent__);
cc();

第一个弹出来的是window 这个好理解 cc是全局的 作用域在window下
但是第2个xx 还是winow, xx的作用域不是在cc里面了吗 为何window alert(xx.__parent__ === cc.__parent__);为 true
...全文
170 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
wtcsy 2010-02-09
  • 打赏
  • 举报
回复
引用 6 楼 azraeln 的回复:
因为 "undefined" === "undefined"

请用firefox测试
ie下无法访问__parent__属性...........
nobody@noone 2010-02-09
  • 打赏
  • 举报
回复
因为 "undefined" === "undefined"
wtcsy 2010-02-09
  • 打赏
  • 举报
回复
var  vvv = "1";
(function(){
alert(this===window) //true
function cc(){
var vv = "1";
function xx(){
alert(xx.__parent__ === cc.__parent__);
}
}
alert(cc.__parent__); //null
})();
wtcsy 2010-02-09
  • 打赏
  • 举报
回复
不对噢
this只是指向他的对象 与定义作用域无关吧
sohighthesky 2010-02-09
  • 打赏
  • 举报
回复

var vvv = "1";
function cc(){
var vv = "1";
function xx(){

}
alert(this==window)
alert(xx.__parent__);
alert(xx.__parent__ === cc.__parent__);
}
alert(this==window)
alert(cc.__parent__);
cc();
wtcsy 2010-02-09
  • 打赏
  • 举报
回复
你这是什么意思?


回复内容太短了! 回复内容太短了! 回复内容太短了! 回复内容太短了! 回复内容太短了! 回复内容太短了!














xiaojing7 2010-02-09
  • 打赏
  • 举报
回复
 this.xx=function(){

}
wtcsy 2010-02-09
  • 打赏
  • 举报
回复
恩 有道理..............
dh20156 2010-02-09
  • 打赏
  • 举报
回复
就是FF下的__parent__不能在嵌套函数中正确的执行,你问为什么那里不是返回null,那我也想知道,你为什么认为那里应该返回null。

在FF下测试的前提已经不对了,所以,不需要继续问这个问题了,你自己知道那里的scope object是什么就可以了,如果一定要测试,可以按#14楼的方法,到Rhino下去测试。
wtcsy 2010-02-09
  • 打赏
  • 举报
回复
这个看的不是很懂
但你想表达的意思是说 firefox下 调用函数的__parent__属性 总是返回window or null
那为什么
var vvv = "1";
function cc(){
var vv = "1";
function xx(){

}
alert(xx.__parent__); //这里不是返回的 null了.........
alert(xx.__parent__ === cc.__parent__);
}
alert(cc.__parent__);
cc();
浴火_凤凰 2010-02-09
  • 打赏
  • 举报
回复
只是听说过变量有局部作用域,还没有听说函数有局部作用域?是不是这个原因呢?
dh20156 2010-02-09
  • 打赏
  • 举报
回复
Unfortunately, the __parent__ property does not work with nested functions when used with Firefox (SpiderMonkey). When __parent__ is used with Firefox, it always returns either the Window object or null.
dh20156 2010-02-09
  • 打赏
  • 举报
回复
The JavaScript language does not provide you with a method of accessing a function’s Variable object. The only way to access this object is by taking advantage of the __parent__ magic property. For example, the following script (which works in Firefox) returns the Global scope object:

1: <script type="text/javascript">

2:

3: function doSomething() {}

4:

5: alert(doSomething.__parent__); // returns Window

6:

7: </script>

8:

The Global scope object in the case of a browser is the Window object. All global variables become properties of the Window object.

Unfortunately, the __parent__ property does not work with nested functions when used with Firefox (SpiderMonkey). When __parent__ is used with Firefox, it always returns either the Window object or null. If you want to use __parent__ with nested functions, then you need to use Rhino.

Rhino is the Java version of the Mozilla JavaScript engine. You can download Rhino from http://developer.mozilla.org/en/docs/Rhino. To get Rhino to work in Windows, I had to follow the instructions from the following blog entry by Luke Maciak: http://www.terminally-incoherent.com/blog/2008/01/08/rhino-scripting-java-with-javascript.

After Rhino is setup, you can execute JavaScript statements from the command line. I executed the following commands:

1: function f()

2:

3: {

4:

5: var bob='hello';

6:

7: var inner = function(){};

8:

9: var parent = inner.__parent__;

10:

11: var contents = '';

12:

13: for (k in parent) contents += k + ' ';

14:

15: print(contents);

16:

17: }

18:

19: f();

The script above contains a function named f(). This function contains an inner function named inner. The __parent__ property of the inner function returns f’s Variable object. Therefore, when the script is executed, the following list of items is printed:

arguments bob contents parent inner

These are the properties that you would expect for the f function’s Variable object. The first items, represents the parameters passed to the f() function. The remaining items correspond to each of the f() function’s local variables.
freespacezjx 2010-02-09
  • 打赏
  • 举报
回复
额,学习...................................................
wtcsy 2010-02-09
  • 打赏
  • 举报
回复
引用 10 楼 freespacezjx 的回复:
js 中的函数都可以看作一个对象,函数都是继承Object的,应该是由于这一点,上面的cc.parent===xx.parent;

你说的不对哦

函数是可以看成一个对象 但是他是先继承的Function的prototype 然后在是继承到Object的prototye

Object.prototype.a="a";
Function.prototype.a="aa";
function xx(){}
alert(xx.a)
delete Function.prototype.a
alert(xx.a)


另外 是__parent__属性 不是 parent属性
这个属性 很特殊(函数的[[scope]] 属性,不访问,在firefox下提供__parent__属性来访问) 它引用的是函数定义时的作用域链
这个与继承什么的无关 只与函数定义时候的作用域有关

ps:这个眼睛是卡卡西 或者说是带土的眼睛 不是鼬或者zz的
freespacezjx 2010-02-09
  • 打赏
  • 举报
回复
o,楼主的写轮眼很帅,很喜欢 HOHO
freespacezjx 2010-02-09
  • 打赏
  • 举报
回复
js 中的函数都可以看作一个对象,函数都是继承Object的,应该是由于这一点,上面的cc.parent===xx.parent;
becameaeagle 2010-02-09
  • 打赏
  • 举报
回复
路过学习
个人还第一次见到这种function套用法
顶下
KK3K2005 2010-02-09
  • 打赏
  • 举报
回复
没有被this之类关联的变量作用域都是window下啊
全局和非全局 只是影响可见范围 和作用域应该没关系吧
个人理解

87,910

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧