When a function is invoked as a function rather that as a method, the this keyword refers to the global object. Confusingly, this is true even when a nested function is invoked (as a function) within a containing method that was invoked as a method: the this keyword has one value in the containing function but (counterintuitively) refers to the global object within the body of the nested function.
《权威指南》8.4节倒数第二段求教这一段话的意思?中文版的我没看懂。先谢谢大家了!
《权威指南》8.4节倒数第二段求教这一段话的意思?中文版的我没看懂。先谢谢大家了!
解决方案 »
- 问个关于file表单的问题
- js alert 换行问题
- 我需要一个像鼠标右键后出来菜单的[ 刷新(R) ]效果,因window.location.reload()不够用
- 急~随页面上下浮动的“top”是怎么弄出来的啊~
- 关于模态窗口的传值问题。新手菜问题,谢谢答复。
- 火急,在子窗怎样关闭整个框架窗口,在线等待
- 一个一直深信CSDN没有解决不了的问题的人的一个一直没有解决的问题(请高手指教!在线等待!)
- 各位老大帮忙,我中了恶毒网站锁注册表
- 为什么window.location.href都能访问到"http://.../?aa=1&bb=1" ,但是post却访问不到呢?
- angular4集成ztree,单击事件不能调用函数
- Flash+JS云标签免费下载
- JS截取问题,有会的进来一下
函数体内都存在一个关键字(类似指针)“this”,未运行“this”指向该函数的原型prototype,运行了则“this”指向(指代)调用该函数的对象。例如:
1、function fun(){this.x=3};var f=new fun;alert(f.x);该函数未运行,this指向它的原型,通过new算符派生出来的实例“f”继承了原型“this”的属性“x”。2、function fun(){alert(this===widnow)};fun();该this就是window,因为window引用了该函数(fun()其实window.fun()就如setTimeout()其实window.setTimeout()一样),同理匿名函数的this也指向window。
3、obj={fun:function(){alert(this===obj)}};obj.fun();该this就是obj,因为是obj引用了该函数。
function print() {
for (var i = 0; i < arguments.length; ++i) {
document.write(arguments[i], " ");
}
document.write("<br/>");
}function func() {
print( "this of Func:", this, "this == window:", this == window );
function nestedFunc() {
print( "this of nestedFunc:", this, "this == window:", this == window );
}
nestedFunc();
}print("<b>call by function:</b>");
func();print();print("<b>call by method:</b>");
func.call(new Object);