this
this
window
this === window
true
var myVar = 'my Day';
undefined
myVar
"my Day"
this.myVar
"my Day"
window.myVar
"my Day"
var a = {}
undefined
a.name = 'suqing'
"suqing"
var a = { name: 'suqing', context: this }
undefined
a.context
a.context === window
true
function foo(){ // something }
undefined
foo()
undefined
foo.trueExpertise = 'beginner';
"beginner"
foo.trueExpertise
"beginner"
foo.this
undefined
function whatIsThis(){ return this; }
undefined
whatIsThis()
(function(){ return this; })()
(function(){ 'use strict'; return this; })()
undefined
(function(self){ 'use strict'; return this; })(this)
undefined
(function(self){ 'use strict'; return self; })(this)
对象的this
var a = { name: 'suqing', getContext: function(){ return this; } }
undefined
a.getContext()
var d = { name: 'yefeng' }
undefined
d.getContext = a.getContext
function (){ return this; }
d.getContext()
var foo = a.getContext
undefined
foo
function (){ return this; }
foo()
a.getContext()
d.getContext()
foo()
var foo = a.getContext
undefined
foo()
构造器
function Friend() { this.area = area; this.favLan = favLan; this.context = this; }
undefined
function Friend(area, favLan) { this.area = area; this.favLan = favLan; this.context = this; }
undefined
var frnd1 = new Friend('client','js')
undefined
frnd1.context
var frnd2 = new Friend('server','java')
undefined
frnd2.context
Friend('server', 'java')
undefined
prototype链
var dad = { fatherName: 'Dad' }
undefined
var child = Object.create(dad)
undefined
child.fatherName
"Dad"
child.whoIsYourDad = function(){ return this.fatherName; }
function (){ return this.fatherName; }
child.whoIsYourDad()
"Dad"
参数this
function getMenu(){ return this; }
undefined
var devMenu = { menu: 'pizza' }
undefined
var forDev = getMenu.bind(devMenu)
undefined
forDev()
Object {menu: "pizza"}
var hrMenu = { menu: 'salad' }
undefined
var forHR = getMenu.bind(hrMenu)
undefined
forHR()
Object {menu: "salad"}
getMenu.call
function call() { [native code] }
getMenu.apply
function apply() { [native code] }
getMenu.apply(hrMenu)
Object {menu: "salad"}
getMenu.call(hrMenu)
Object {menu: "salad"}
setTimeout
var a = { getContext: function(){ setTimeout( function(){ console.log(this); }, 10) } }
undefined
a.getContext()
undefined
var a = { getContext: function(){ setTimeout( function(){ console.log(this); }, 4000) } }
undefined
a.getContext()
undefined
DOM event handler
document.getElementById('s_rp_words').addEventListener('click', function(){ console.log(this); })
undefined
相关推荐
深度理解js中this的指向问题
NULL 博文链接:https://wv1124.iteye.com/blog/505236
深化理解Javascript中this的作用域_.docx
两句话理解js中的this2017 年 11 17前: 直都搞不清javascript中this的指向,你不知道的javascript(上卷)这本书中有3章都是
深入理解JavaScript系列(13):This? Yes, this! 深入理解JavaScript系列(14):作用域链(Scope Chain) 深入理解JavaScript系列(15):函数(Functions) 深入理解JavaScript系列(16):闭包(Closures) ...
本文将介绍 JavaScript 中 this 的用法,从而帮助开发者更好地理解并掌握 this 的使用。 一、在全局作用域中使用 this 在全局作用域中使用 this 会返回全局对象,在浏览器环境下是 window 对象, 在 Node.js 环境...
本文结合网络收集整理,尝试以一种简易的方式阐述JavaScript中this的用法,希望对大家关于JavaScript中this用法的快速理解有所帮助。正文1. this用法实例 代码如下:window.color = “red”; var o = { color: “blue...
深度解析 和this
JavaScript中this的指向还没搞明白?来这看看 你就懂啦~
高手详解javascript中的this指针
详解Javascript 中的this指针
Javascript 中 this指向
深入理解JavaScript系列(13):This Yes this 深入理解JavaScript系列(14):作用域链 Scope Chain 深入理解JavaScript系列(15):函数(Functions) 深入理解JavaScript系列(16):闭包(Closures) 深入...
js原生态函数中使用jQuery中的 $(this)无效的解决方法.docx
this指触发事件的对象,接下来为大家分享下javascript中onclick(this)的用法,感兴趣的朋友可以参考下哈,希望对你有所帮助
JavaScript中this的使用,简单理解,快速学会
对大多数有OOP开发经验的开发人员来说this是当前作用域中引用普通元素的标识符,但是在Javascript中它却显得古灵精怪的,因为它不是固定不变的,而是随着它的执行环境的改变而改变。在Javascript中this总是指向调用...
js上下文理解js上下文理解js上下文理解js上下文理解js上下文理解js上下文理解js上下文理解js上下文理解js上下文理解js上下文理解
this是js的一个关键字,随着函数使用场合不同,this的值会发生变化。但是总有一个原则,那就是this指的是调用函数的那个对象。 1、纯粹函数调用。 function test() { this.x = 1; alert(x); } test(); ...
JavaScript程序设计javascript中this的指向问题共6页.pdf.zip