javaScript中this的指向?
javaScript中this对象是在运行时基于函数的执行环境绑定的,在全局函数中,this等于window,而当函数被作为某个对象的方法调用时,this等于那个对象。
但在实际中,代码环境复杂,this的指向并非那么直接判断出来。下面来做一下总结。
1,全局执行环境下的普通函数
function f1 () {
console.log(this)
}
function f2 () {
'use strict'
console.log(this)
}
f1() // window
f2() // undefined
分为严格模式和非严格模式。非严格模式下等于window,严格模式下为undefined。
2,事件对象中的this
this指的是事件对象本身,是指event.target
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div id="div1">我是一个div</div>
<script>
window.id = 'window'
document.getElementById('div1').onclick = function (){
alert(this.id) // 输出div1 }
</script>
</body>
</html>
3,对象中的方法引用
const foo = {
bar: ,
fn: function() {
console.log(this)
console.log(this.bar)
}
}
foo.fn() // this指的是foo
var fn1 = foo.fn
fn1() // this指的是window
4,构造函数中的this
// this指instance
function Foo() {
this.bar = "ceshi"
}
const instance = new Foo()
console.log(instance.bar) // this指的是 {}
function Foo(){
this.user = "ceshi"
const o = {}
return o
}
const instance = new Foo()
console.log(instance.user)
这里的this之所以不同是由构造返回值是否是对象导致的
5,丢失的this
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div id="div1">我是一个div</div>
<script>
let getId = document.getElementById
console.log(getId('div1')) // 报错,报错这是因为document.getElementById方法内的this指向window导致的
// 修复
document.getElementById = (function(func){
return function(){
return func.apply(document,arguments)
}
})(document.getElementById)
let getId = document.getElementById
console.log(getId('div1'))
</script>
</body>
</html>
6,借助bind、Object.prototype.call和Object.prototype.apply改变this的指向
Object.prototype.call和Object.prototype.apply的是后者参数为数组,前者为多个参数。
用代码来总结:
const target = {}
fn.call(target, 'arg1', 'arg2')
相当于:
const target = {}
fn.apply(target, ['arg1', 'arg2'])
相当于:
const target = {}
fn.bind(target, 'arg1', 'arg2')()
借助call改变
const foo = {
name: 'ceshi',
logName: function() {
console.log(this.name)
}
}
const bar = {
name: 'mike'
}
console.log(foo.logName.call(bar)) // mike
7,其他一些复杂的场景
const foo = {
fn: function () {
setTimeout(function () {
console.log(this)
})
}
}
foo.fn() // this===window
let,const不会挂在window上面,而var可以
const a =
let aa =
var b =
function too(){
console.log(this.a)
console.log(this.aa)
console.log(this.b)
}
too()
对象嵌套中的this
const person = {
name: 'ceshi',
brother: {
name: 'zhen',
fn: function() {
return this.name
}
}
}
console.log(person.brother.fn()) // zhen
更复杂的情景
const o1 = {
text: 'o1',
fn: function () {
return this.text
}
}
const o2 = {
text: 'o2',
fn: function () {
return o1.fn()
}
}
const o3 = {
text: 'o3',
fn: function () {
var fn = o1.fn
return fn()
}
}
console.log(o1.fn()) // o1
console.log(o2.fn()) // o1
console.log(o3.fn()) // undefined
箭头函数的this提前绑定好
const foo = {
fn: function () {
setTimeout(() => {
console.log(this)
})
}
}
console.log(foo.fn()) // {fn: ƒ}
8,通过 call、apply、bind 绑定的情况称为显式绑定;根据调用关系确定的 this指向称为隐式绑定。那么哪一个优先级更高呢?
function foo (a) {
console.log(this.a)
}
const obj1 = {
a: ,
foo: foo
}
const obj2 = {
a: ,
foo: foo
}
obj1.foo.call(obj2) //
obj2.foo.call(obj1) //
显式绑定的优先级高于隐式绑定。
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指向的问题,望能给大家提供帮助! ...
随机推荐
- winform Anchor和Dock属性
在设计窗体时,这两个属性特别有用,如果用户认为改变窗口的大小并不容易,应确保窗口看起来不显得很乱,并编写许多代码行来达到这个目的,许多程序解决这个问题是地,都是禁止给窗口重新设置大小,这显然是解决问题 ...
- LeetCode练题——66. Plus one
1.题目 66. Plus One——easy Given a non-empty array of digits representing a non-negative integer, plus ...
- Mongodb 分片原理
1.主从mongodb 模式 类似,MySQL的主从配置 参照:https://blog.csdn.net/liusong0605/article/details/11551699 mongoDB有 ...
- python opencv:使用鼠标当做画笔
鼠标事件 import cv2 events=[i for i in dir(cv2) if 'EVENT'in i] print events 双击画圆圈 import cv2 import num ...
- 《创业者技能树》--创业课--14天复盘第一课candy2--HHR计划
1,厉害的人是如何分析问题的?
- MySQL数据库--基础简述
MySQL数据库--基础简述 1.15.1 MySQL简介 Mysql是最流行的RDBMS(Relational Database Management System:关系数据库管理系统),特别是在W ...
- 2016-2017学年第三次测试赛 问题 F: 签到题
问题 F: 签到题 时间限制: 1 Sec 内存限制: 128 MB提交: 80 解决: 28 提交统计讨论版 题目描述 在计算机网络考试中, 黑帅男神看到一个将IP网络分类的题, 精通C++的他 ...
- Java面向对象编程 -1.4
对象内存分析 对象实例化操作初步分析 Java之中类属于引用数据类型,引用数据类型最大的困难之处在于要进行内存的管理,同时在进行操作的时候也会有内存关系的变化. 所以本次针对于之前的程序的内存关系进行 ...
- ubuntu 16.04 XDRP实现Windows远程访问
如何通过XDRP实现Windows远程访问 下面才是本文的重点,本文主要是讲xrdp在目前最新版Ubuntu 16.04下,如果实现Windows远程访问.网上也很多相关教程,但是都需要安装xfac4 ...
- Session服务器之Session复制!
全部运行在Tomcat下 第一台主机:192.168.200.131 安装nginx 修改hosts文件 [root@localhost ~]# vim /etc/hosts 192.168.200 ...