- 普通函数

  | 具名普通函数、匿名普通函数,在不作为对象的属性值的情况下,其内部的 this 总是指向代码运行环境下的全局对象 ( 例如,浏览器中的 window )。

示例:

    (function() {
console.log(this); // window
(function() {
console.log(this); // window
(function() {
console.log(this); // window
})()
})()
})()

  

  | 普通函数,均可以通过其 bind、call、apply 方法 来改变其内部 this 的指向。

示例:

    (function() {
const func = (function() { console.log(this) }).bind('hello')
const obj = {
func,
func1: (function() { console.log(this) }).bind('hello'),
func2: (function F() { console.log(this) }).bind('hello')
}
func() // String {'hello'}
obj.func() // String {'hello'}
obj.func1() // String {'hello'}
obj.func2() // String {'hello'}
})()

  

  | 当普通函数( 具名的、匿名的、外部定义的方法 ),作为对象的属性值被引用的时候,其内部的 this 指向该属性所直接归属的对象 。

示例:

    (function() {
const func = function() { console.log(this) }
const obj = {
func,
func1: function F() { console.log(this) },
func2() { console.log(this) },
param: {
func,
func1: function F() { console.log(this) },
func2() { console.log(this) }
}
}
func() // window
obj.func() // obj
obj.func1() // obj
obj.func2() // obj
obj.param.func() // obj.param
obj.param.func1() // obj.param
obj.param.func2() // obj.param
})()

  


- 箭头函数

  | 箭头函数,不管是作为独立的方法 或是 作为对象的属性值,其内部的 this 均指向 该箭头函数被定义时所在的上下文中对应的 this。

示例:

    (function() {
/** 外层作用域 */
const arrowfunc = () => console.log(this) console.log('-- 外层作用域 --');
console.log(this); // String {'hello'}
arrowfunc(); // String {'hello'} (function() {
/** 内层作用域 */
const arrowfunc1 = () => console.log(this) console.log('-- 内层作用域 --');
console.log(this); // String {'world'}
arrowfunc() // String {'hello'}
arrowfunc1() // String {'world'} /** 函数作为对象属性值 */
const obj = {
arrowfunc,
arrowfunc1,
param: {
arrowfunc,
arrowfunc1,
arrowfunc2: () => console.log(this)
}
} console.log('-- 函数作为对象属性值 --');
obj.arrowfunc() // String {'hello'}
obj.arrowfunc1() // String {'world'}
obj.param.arrowfunc() // String {'hello'}
obj.param.arrowfunc1() // String {'world'}
obj.param.arrowfunc2() // String {'world'}
}).bind('world')()
}).bind('hello')()

  

  | 箭头函数 也有 bind、call、apply 方法,与普通函数一样可以通过这三个方法预设箭头函数的入参值。

    试图通过这三个方法改变箭头函数内部 this 的指向,虽不会报错但却是无效的。

示例:

    (function() {
console.log(this); // String {'hello'}
(() => {
console.log(this); // String {'hello'}
(() => {
console.log(this) // String {'hello'}
}).bind('bbb')()
}).bind('aaa')(); ((a, b, c) => {
console.log(this) // String {'hello'}
console.log(a) // a
console.log(b) // b
console.log(c) // c
}).bind(null, 1, 2)(3)
}).bind('hello')()

  

  | 附:

* 箭头函数不能作为构造函数使用,强制使用 new 运算符作用在箭头函数上,将会报如下错误

         new (() => {}) // Uncaught TypeError: (intermediate value) is not a constructor

  

* 箭头函数内部没有定义 arguments 变量,箭头函数所在的作用域也不存在 arguments 的情况下,应用该变量会报错。

        (function() {
((a) => {
console.log(a) // 1
console.log(arguments) // Arguments ['hello']
})(1)
})('hello'); (() => {
console.log(arguments) // Uncaught ReferenceError: arguments is not defined
})();

* 普通函数都有原型属性 prototype,箭头函数没有这个属性。

        (function() {}).prototype // {constructor: ƒ}
(() => {}).prototype // undefined

  

 
 

js函数( 普通函数、箭头函数 ) 内部this的指向的更多相关文章

  1. 前端分享----JS异步编程+ES6箭头函数

    前端分享----JS异步编程+ES6箭头函数 ##概述Javascript语言的执行环境是"单线程"(single thread).所谓"单线程",就是指一次只 ...

  2. js中this,箭头函数和普通函数

    四种基本用法 1. 一般方法中,this代指全局对象 window 2. 作为对象方法调用,this代指当前对象 3. 作为构造函数调用,this 指代new 出的对象 function test() ...

  3. JS中generater和箭头函数

    generater跟函数很像: function* fn(x){ yield x; yield x++; return x;} 如上所示,generater用function*定义,可以用yield返 ...

  4. JS ES6中的箭头函数(Arrow Functions)使用

    转载这篇ES6的箭头函数方便自己查阅. ES6可以使用“箭头”(=>)定义函数,注意是函数,不要使用这种方式定义类(构造器). 一.语法 基础语法 (参数1, 参数2, …, 参数N) => ...

  5. js中箭头函数 及 针对箭头函数this指向问题引出的单体模式

    ES6允许使用“箭头”(=>)定义函数 var f = a = > a //等同于 var f = function(a){ return a; } 如果箭头函数不需要参数或需要多个参数, ...

  6. JavaScript的函数申明、函数表达式、箭头函数

    JavaScript中的函数可以通过几种方式创建,如下. // 函数声明 function getName() { return 'Michael' } // 函数表达式 const getName ...

  7. es6 入坑笔记(二)---函数扩展,箭头函数,扩展运算符...

    函数扩展 1.函数可以有默认值 function demo( a = 10,b ){} 2.函数可以使用解构 function demo( { a = 0,b = 0 } = {} ){ } 3.函数 ...

  8. 关于ES6-{块级作用域 let const 解构赋值 数组 字符串 函数的扩展 箭头函数}

    关于ES6 块级作用域 任何一对花括号({})中的语句集都属于一个块,在块中声明的变量在代码块外都是不可访问的,称之为块级作用域,ES5以前没有块级作用域 let let 是ES6新增的声明变量的一种 ...

  9. 函数的扩展——箭头函数this的使用

    箭头函数中的this指向的是定义时的this,而不是执行时的的this . 举例: 案例中,我们的obj对象中有一个属性x和一个属性show( )方法,show( )通过this打印出x的值,结果是u ...

  10. react 中 函数bind 和箭头函数

    用bind形式 方便测试,含有this时候最好用bind形 其他情况用箭头函数 含有this的时候也可以用箭头函数

随机推荐

  1. MySQL-4-DDL

    DDL:数据定义语言 创建create 创建库 语法:create database [if not exists]库名 # 创建库 CREATE DATABASE IF NOT EXISTS boo ...

  2. python小题目练习(13)

    题目:封装用户的上网行为 实现代码: """Author:mllContent:封装用户的上网行为Date:2020-01-19"""def ...

  3. NC19115 选择颜色

    NC19115 选择颜色 题目 题目描述 \(n\) 个人排成一个环形,每个人要从 \(c\) 种颜色中选择一个. 牛牛希望相邻的人选择的颜色是不同的 问有多少种方案. 输出方案数对 \(10007\ ...

  4. SLF4J 日志门面

    目录 01.简单介绍 02.日志级别 03.入门案例 03.动态打印 04.异常打印 05.日志集成 06.集成 logback 07.集成 slf4j-nop 08.集成 log4j 09.集成 j ...

  5. MQ系列2:消息中间件的技术选型

    1 背景 在高并发.高消息吞吐的互联网场景中,我们经常会使用消息队列(Message Queue)作为基础设施,在服务端架构中担当消息中转.消息削峰.事务异步处理 等职能. 对于那些不需要实时响应的的 ...

  6. LNMP架构及DISCUZ论坛部署

    1)(5分)服务器IP地址规划:client:12.0.0.12/24,网关服务器:ens36:12.0.0.1/24.ens33:172.16.10.1/24,Web1:172.16.10.10/2 ...

  7. Template -「整体二分」

    写的简单.主要是留给自己做复习资料. 「BZOJ1901」Dynamic Rankings. 给定一个含有 \(n\) 个数的序列 \(a_1,a_2 \dots a_n\),需要支持两种操作: Q ...

  8. Linux 加密安全和私有CA的搭建方法

    常用安全技术 3A: 认证:身份确认 授权:权限分配 审计:监控做了什么 安全通信 加密算法和协议 对称加密: 非对称加密 单向加密:哈希(hash)加密 认证协议 对称加密: 加密和解密使用的是同一 ...

  9. api.versioning 版本控制 自动识别最高版本和多Area但同名Contoller问题解决办法

    Microsoft.AspNetCore.Mvc.Versioning //引入程序集 .net core 下面api的版本控制作用不需要多说,可以查阅https://www.cnblogs.com/ ...

  10. Fibonacci Nim

    目录 题意 题解 相关 Ref 题意 [COCI2010-2011#4] HRPA 取石子,但是: 先手第一次可取任意多个石子 此外每次可取的石子的个数,至少为 \(1\) ,至多为上一轮对方所取个数 ...