<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title></title></head>
<body>
<script type="text/javascript">
function ss(){
this.a='xx';
}
ss();
alert(a);
//==================上面的执行方式 跟下面的执行方式是一样的吗
//this.a = 'xx'
//alert(a)
</script>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title></title></head>
<body>
<script type="text/javascript">
function ss(){
this.a='xx';
}
ss();
alert(a);
//==================上面的执行方式 跟下面的执行方式是一样的吗
//this.a = 'xx'
//alert(a)
</script>
</body>
</html>
那下面的该怎么解释了
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title></title></head>
<body>
<script type="text/javascript">
function o(){
this.b = 'b';
}
function oo(){
o();
this.a = 'a'
}
function ooo(){
this.a = 'a';
this.b = 'b';
}
//alert(new oo().a); //正确
alert(new oo().b) //按地反
//alert(new ooo().b) //正确
</script>
</body>
</html>
alert(window.b); // 调用oo执行o函数 将this指定在window 有啥问题?
你去看一下javascript类,就能解决你的疑问了
alert( typeof (Object) );
也即Javascript里的函数实际上是类型为Function的特殊对象。
===============================================================================
上面都是copy过来的 但是我觉得上面说的都是对的
传诵门
主要是第2个一个例子
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title></title></head>
<body>
<script type="text/javascript">
function xx(){
function xxx(){
this.a ='a';
}
xxx();
}
xx();
alert(a);
</script>
</body>
</html>=========================================================================
验证第一条
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title></title></head>
<body>
<script type="text/javascript">
function o(){
this.b = 'b';
}
function oo(){
this.cc = o;
this.cc();
//这个地方 o里面的this就指向了this.cc中的this的指向了 至于这个this的指象还不明白,哪个对象调用他 就志向哪个
//o();
this.a = 'a'
}
function ooo(){
this.a = 'a';
this.b = 'b';
}
//alert(new oo().a); //正确
alert(new oo().b) //当是新创建一个对象的时候 就指向先创建的对象
//alert(new ooo().b) //正确
var ss = {}
oo.call(ss);
alert(ss.b) //当是ss调用的时候就指向了ss
</script>
</body>
</html>个人理解 欢迎指正错误.........
也来说两句.其实楼主所谓的迷惑很简单 就是没有找对this的调用者JS语句执行的全部环境 称为execution context
而this value 和 scopeChain是其中的两个不同部分
而这两部分作用各不相同比如 this.val = A;
在没有执行上下文的时候 是无法确实执行后的结果是什么但如果指定了上下文 那么就可以从this value得到this的值 可以从scopeChain里
查找到变量名A的值**this value的确定 是跟调用函数 使用的引用息息相关比如 function fun(){ this.val = A }var a = fun;
var o = {};
o.b = fun;a(); //这个时候调用 fun this指向的就是window
o.b(); // 这个时候fun 里的this 就是指向o所以楼主的问题 只是执行函数 this 默认指向在window里
function oo(){
this.cc = o;
this.cc();
//这个地方 o里面的this就指向了this.cc中的this的指向了 至于这个this的指象还不明白,哪个对象调用他 就志向哪个
//o();
this.a = 'a'
}
想问下 你这个转换 跟楼主#2的例子 有关系么? 个人不是很理解 想请教下
========================================this.cc()是执行函数了 (对吧)
cc = o //o为function o(){this.b = 'b';}
o里面的this指向调用他的对象
我是让他指向this.cc的this当有对象(假如是ss)掉用cc方法的时候o里面的this就指向了ss就是这个意思个人理解...................................