首页 > 代码库 > JavaScript的this用法
JavaScript的this用法
1. 全局代码中的this
this在全局上下文中,它的值是全局对象本身(Global Object),在浏览器中就是Window Object,如下图示。
看下面几个例子:
//Global scope
//The implicit property of the global object
var foo1 = "abc";
console.log(this.foo1 == window.foo1); console.log(foo1); console.log(this.foo1); console.log(window.foo1); //true abc abc abc
//The implicit property of the global object
foo2 = "def";
console.log(this.foo2 == window.foo2); console.log(foo2); console.log(this.foo2); console.log(window.foo2); //true def def def
//The explicit property of the global object
this.foo3 = "gh";
console.log(this.foo3 == window.foo3); console.log(foo3); console.log(this.foo3); console.log(window.foo3); //true gh gh gh
//The implicit property of the global object
var foo1 = "abc";
console.log(this.foo1 == window.foo1); console.log(foo1); console.log(this.foo1); console.log(window.foo1); //true abc abc abc
//The implicit property of the global object
foo2 = "def";
console.log(this.foo2 == window.foo2); console.log(foo2); console.log(this.foo2); console.log(window.foo2); //true def def def
//The explicit property of the global object
this.foo3 = "gh";
console.log(this.foo3 == window.foo3); console.log(foo3); console.log(this.foo3); console.log(window.foo3); //true gh gh gh
2.
JavaScript的this用法
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。