drop: (item, monitor) => addTaskToSprint(item.id, monitor, drop)
length: 2
name: "drop"
arguments: (...)
caller: (...)
[[FunctionLocation]]: SprintCard.jsx:45
[[Prototype]]: ƒ ()
[[Scopes]]: Scopes[3]
0: Closure (SprintCard)
ItemType: "Task Card"
addTaskToSprint: (id, monitor, dropTargetProps, task) => {…}
drop: ƒ ()
我的api响应如下,
我需要访问[[Scopes]],以便获得ItemType变量
drop是一个函数,所以像这样访问作用域(spec.drop.scopes(不会起的作用
当我console.log(spec.drop(时,它只显示函数声明
那么有什么方法可以访问作用域吗?
[[Scopes]]不是对象的一部分,它是devtools用来显示函数关闭的环境记录对象的类别。drop
函数可以访问ItemType
,但其他代码不能通过drop
访问ItemType
,除非drop
以某种方式提供该访问。更普遍地说,有权访问函数的代码不一定有权访问该函数可以访问的东西(事实上,它通常没有(。
这里有一个更简单的例子:
function counter() {
let value = 0;
const increment = () => ++value;
return increment;
}
function example() {
const increment = counter();
console.log(increment()); // Shows "1"
console.log(increment()); // Shows "2" -- that is, the `increment`
// function closes over `value` so it
// can use it to return an incrementing
// counter
// There's no way for code here to directly access the `value` that
// `increment` closes over.
}
example();
increment
函数可以访问value
,但example
函数不能访问,即使它可以访问increment
。