Skip to content
On this page

箭头函数

js
const fn = () => {
	console.log('call', this)
}
// new fn() Uncaught TypeError: fn is not a constructor
let obj = {
	fn1: fn
}
obj.fn1() // Window
obj = {
	name: 'he',
	fn1() {
		const fn = () => {
            console.log('call', this)
        }
        fn()
	}
}
obj.fn1() // {name: 'he', fn1: ƒ}

箭头函数的特点:

  • this 指向定义时所在的对象,而不是调用时所在的对象
  • 不可以当做构造函数
  • 不可以使用 arguments 对象

Function.prototype.toString() (ES 10 / ES 2019)

返回源代码中的实际文本片段(空格和注释都保留了)

js
function foo() {
	// todo
	console.log('in foo')
}
console.log(foo.toString())
//function foo() {
//	// todo
//	console.log('in foo')
//}

可选的 Catch Binding(ES 10 / ES 2019)

省略 catch 绑定的参数和括号

js
// 以前catch 需要保留参数的
try {
	// do something
} catch(error) {
	console.log(error)
}
// 新特性
try {
	// do something
} catch {
	// do something
}

Released under the MIT License.