函数作用域确定于函数定义期间

1. 函数作用域确定于函数定义期间

let count = 1
function fooInner() {
  console.log(count) // zdz-log
}
function foo() {
  let count = 2
  fooInner()
}
foo() // 1 箭头函数和普通函数一样

image.png

2. 构造实例后修改原型,已经创建的实例不受影响

function Foo() {}
Foo.prototype = {
  name: 'old'
}
let oldFoo = new Foo()
console.log(oldFoo.name) // old

Foo.prototype = {
  name: 'new'
}
let newFoo = new Foo()
console.log(newFoo.name) // new
![image.png](https://upload-images.jianshu.io/upload_images/26022476-7ed11583b033141a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

3.函数形式 作用域链机制

function foo() {
  console.log('old') // zdz-log
}
let obj = {
  foo: foo
}

let objN = {
  foo: () => foo()
}
foo = () => console.log('new') // zdz-log

obj.foo()
objN.foo()

image.png

posted @ 2024-04-15 10:30  story.Write(z)  阅读(4)  评论(0编辑  收藏  举报