JavaScript 中 this的指向
this 一方面便利了让大家在JS开发当, 但是另一方面让开发者头痛的是不清楚this 指代什么.
指向全局Window:
<script>
console.log(this);
</script>
Function attached to global project. So "this" in the project will point to global project.
<script>
function calculateAge(year) {
console.log(2018 - year);
console.log(this);
}
</script>
在object的function中的innerFunction
一些开发者认为this 应该是指向object John, 因为还在John这个 object 的scope chain中.
JS Rules:
When a regular function code called, then the default object is the window object. This is how it happens in the browser.
InnerFunction is not a method, because the method is called calculateAge. Method of the John object.
InnerFunction although is written insdie of a method, it is still a regular function.
在网页中, 当一个普通的function 代码被call时, default object 是 window object.
InnerFunction 这样嵌套在method(John object里面的calculate function), 这样写在method里面的function, 是一个普通的function 而不是method, 所以this 指向全局window.
<script>
var john = {
name: 'John',
yearOfBirth: 1990,
calculateAge: function () {
console.log(this);
console.log(john.yearOfBirth); function innerFunction() {
console.log(this);
} innerFunction();
}
} john.calculateAge();
</script>
指向scope chain(作用域):
calculateAge 的function的"this"会指向当前scope chain作用域(john object)
this object refers to the object than called the method.
<script>
var john = {
name: 'John',
yearOfBirth: 1990,
calculateAge: function() {
console.log (this);
}
} john.calculateAge();
</script>
更多this 原理:
http://www.ruanyifeng.com/blog/2018/06/javascript-this.html
JavaScript 中 this的指向的更多相关文章
- javascript中this的指向
作为一个前端小白在开发中对于this的指向问题有时候总是会模糊,于是花时间研究了一番. 首先this是JS的关键字,this是js函数在运行是生成的一个内部对象,生成的这个this只能在函数内部使用. ...
- Javascript中的this指向。
一.JavaScript中的函数 在了解this指向之前,要先弄明白函数执行时它的执行环境是如何创建的,这样可以更清楚的去理解JavaScript中的this指向. function fn(x,y,n ...
- JavaScript中 this 的指向
很多人都会被JavaScript中this的指向(也就是函数在调用时的调用上下文)弄晕,这里做一下总结: 首先,顶层的this指向全局对象. 函数中的this按照调用方法的不同,其指向也不同: 1.函 ...
- 前端面试之JavaScript中this的指向【待完善!】
JavaScript中this的指向问题! 另一个特殊的对象是 this,它在标准函数和箭头函数中有不同的行为. 在标准函数中, this 引用的是把函数当成方法调用的上下文对象,这时候通常称其为 t ...
- JavaScript中的this指向
this是谁 技术一般水平有限,有什么错的地方,望大家指正. this代指当前对象super调用父类的构造函数,应表会运网数物,加载驱动建立链接执行SQL处理结果,直到现在每想起这三点就能想起我上大学 ...
- Javascript 中的this 指向的对象,你搞清楚了吗?
Javascript 中的this 总让人感到困惑,你能分清以下三种test1(),test2(),test3() 情况下的输出吗? 注:以下Javascript运行环境中为浏览器 //1 this在 ...
- javascript中的this指向问题
在深入学习JavaScript之后,我们越来越多的会遇到函数或者在对象内部中,对于this的指向问题的疑惑,其实基本上每一个编程语言中都有一个this,这个this的指向都是大同小异,你也可以汉化它的 ...
- 谈谈 JavaScript 中的 this 指向问题
JavaScript 中的 this 为一个重难点,它不像静态语言 C#.Java 一样,就表示当前对象.而在 JS 中, this 是运行时确定,而并非定义时就已确定其值. 谈起 this ,必须少 ...
- JavaScript中this的指向问题
this是面向对象语言中一个重要的关键字,理解并掌握该关键字的使用对于我们代码的健壮性及优美性至关重要.而javascript的this又有区别于Java.C#等纯面向对象的语言,这使得this更加扑 ...
- 轻松几句搞定【Javascript中的this指向】问题
this关键字在JavaScript中扮演了至关重要的角色,每次它的出现都伴随着它的指向问题,这也是很多初学者容易出错的地方. 不过,这篇文章将会带你一次性搞定this指向的问题,望能给大家提供帮助! ...
随机推荐
- 1.2socket服务器使用多线程
socket服务器代码如下 # -*- coding: utf-8 -*-from socket import * import time import threading,_thread as th ...
- java.lang.Runnable 源码分析
子接口:RunnableFuture<V>, RunnableScheduledFuture<V> 实现类:AsyncBoxView.ChildState, ForkJoinW ...
- sptring boot 修改默认Banner
一.自定义banner 启动Spring Boot项目时,在控制台或日志中会默认显示一个Banner,如图所示: 在我们的项目中更希望使用自己的Banner,这样看起来更帅写,但是这对于程序员来说并不 ...
- L245
The State Council will lay down new rules that aim to make management compatible with internationall ...
- SpringBoot 添加fastjson
1.先在项目中添加fastjson依赖: <dependency> <groupId>com.alibaba</groupId> <artifactId> ...
- 自动化测试-21.RobotFrameWork配置安装
更新pip python -m pip install --upgrade pip 1安装robotframework --pip install robotframework 2. 安装支持框架的运 ...
- git之自学
- [转] 理解 LSTM 网络
[译] 理解 LSTM 网络 http://www.jianshu.com/p/9dc9f41f0b29 Recurrent Neural Networks 人类并不是每时每刻都从一片空白的大脑开始他 ...
- 【PPT】 Least squares temporal difference learning
最小二次方时序差分学习 原文地址: https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd= ...
- PHPweb应用攻击总结(转)
XSS跨站脚本 概念:恶意攻击者往Web页面里插入恶意html代码,当用户浏览该页之时,嵌入其中Web里面的html代码会被执行,从而达到恶意用户的特殊目的. 危害: 盗取用户COOKIE信息. 跳转 ...