嵌套函数定义在执行的时候,每次生成的是个新函数对象。这个告诉我们,不要在函数组件里定义子函数组件,函数执行的时候子函数组件会被先卸载再挂载的。
示例
const funs1=[],funs2=[];
function funParent() {
funChil1();
funChil2();
function funChil1() {
funs1.push(funChil1);
}
}
function funChil2() {
funs2.push(funChil2);
}
funParent();
funParent();
console.log("fun1:",funs1[0]===funs1[1]);
console.log("fun2:",funs2[0]===funs2[1]);
ECMAScript 标准文档查找过程
- [Function Object [[Call]]](https://262.ecma-international.org/12.0/#sec-ecmascript-function-objects-call-thisargument-argumentslist)
- PrepareForOrdinaryCall
- OrdinaryCallEvaluateBody
- Runtime Semantics: EvaluateBody
- Runtime Semantics: EvaluateFunctionBody
- FunctionDeclarationInstantiation
- b. Let fo be InstantiateFunctionObject of f with argument lexEnv.
- Let F be OrdinaryFunctionCreate(%Function.prototype%, sourceText, FormalParameters, FunctionBody, non-lexical-this, scope)
- Let F be ! OrdinaryObjectCreate(functionPrototype, internalSlotsList).
- b. Let fo be InstantiateFunctionObject of f with argument lexEnv.
- FunctionDeclarationInstantiation