javascript是谁

Who or what is this in JavaScript? It depends on how you’re using it but first let’s talk about what it is. this is a keyword in JavaScript. Think of this as a keyword to refer to the object in context. What does that mean? Think about this sentence. John is going to buy coffee because he ran out of coffee at home. Think about the use of the pronoun he. You could technically write “John is going out to buy coffee because John ran out of coffee at home.” Although the second sentence isn’t grammatically incorrect, you probably wouldn’t write a sentence like that.

谁或什么是this在JavaScript? 这取决于您的使用方式,但首先让我们谈谈它的用途。 this是JavaScript中的关键字。 将this视为在上下文中引用对象的关键字。 那是什么意思? 想想这句话。 约翰要买咖啡,因为他家里没咖啡了。 考虑一下他代词的用法。 您可以从技术上写“约翰要出去买咖啡,因为约翰家里没咖啡了。” 尽管第二句话在语法上不是错误的,但是您可能不会写这样的句子。

So how would we use this in our code? Look at the example below!

那么我们将如何在代码中使用this呢? 看下面的例子!

var person = {
name: ‘Jonathan’
}console.log(this.name + ‘is going out to buy coffee because he forgot’)console.log(person.name + ‘is going out to buy coffee because he forgot’)Either console log works!

So you may be thinking to yourself, this does fit better contextually in our code but does it go beyond that? Well yes, it actually does! Because of how this works, its contextual placement is very important. What do I mean? Think about the example I used before, John is going out to buy coffee because he ran out of coffee at home. You know the pronoun is referring to John because of the context of the pronoun. What if John was going to buy coffee for another person named John? Although it seems unlikely, this also leaves no question for which object is referred to. this also contains the value of the object in reference so it helps even more.

因此,您可能在想自己, this在我们的代码中确实更适合于上下文,但是还可以吗? 好吧,实际上是这样! 由于如何的this作品,它的上下文位置是非常重要的。 我什么意思 想想我以前使用的例子,约翰要去买咖啡,因为他家里没咖啡了。 您知道由于代词的上下文,代词是指约翰。 如果约翰打算为另一个叫约翰的人买咖啡怎么办? 尽管似乎不太可能, this也毫无疑问提到了哪个对象。 this也包含引用对象的值,因此可以提供更多帮助。

Let’s now talk about the context of how this is used. this always refers to an object in hand. What do I mean? If you were to place this inside a function, it would refer to the function. It was used in the global scope, it would refer to the entire window object. For example, try typing this in your Developer Tools in your browser. The entire window object gets returned!

现在让我们来谈谈如何上下文this被使用。 this总是指手中的物体。 我什么意思 如果你放置this函数内部,它会参考功能。 它在全局范围内使用,它将引用整个window对象。 例如,尝试在浏览器的开发人员工具中键入this 。 整个窗口对象被返回!

Conclusion

结论

In summation, this helps us not only because it reads better but also because it leaves no ambiguity for those who read our code (ourselves included) to whatever object is being referred to. Check out the resources below for more information!

总而言之,这不仅对我们有帮助,还因为它读起来更好,而且对于那些阅读我们的代码(包括我们自己)的人来说,对于所引用的任何对象都不会造成歧义。 查看以下资源以获取更多信息!

翻译自: https://medium.com/swlh/javascript-who-is-this-4fa9e8116eb6

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值