ECMAScript 5.1 内部方法 [[Call]]
ECMAScript 5.1 internal method [[Call]]
我需要帮助来理解该算法:
[[Call]]
When the [[Call]] internal method for a Function object F is called with a this value and a list of arguments, the following steps are taken:
- Let funcCtx be the result of establishing a new execution context for function code using the value of F's [[FormalParameters]] internal property, the passed arguments List args, and the this value as described in 10.4.3.
- Let result be the result of evaluating the FunctionBody that is the value of F's [[Code]] internal property. If F does not have a [[Code]] internal property or if its value is an empty FunctionBody, then result is (normal, undefined, empty).
- Exit the execution context funcCtx, restoring the previous execution context.
- If result.type is throw then throw result.value.
- If result.type is return then return result.value.
- Otherwise result.type must be normal. Return undefined.
https://www.ecma-international.org/ecma-262/5.1/#sec-13.2.1
正是我需要在 2,4,5,6 条款中的解释。
关于2:首先,FunctionBody计算的结果是什么意思?它是如何计算的?发生这种情况时没有 [[Code]] 属性 是什么意思?最重要的是,这条记录是什么意思(正常、未定义、空)。
关于4,5,6:result.type是什么意思,result.value?这个值从何而来?对每个点进行解释
P.S如果您投反对票,请解释您这样做的原因!
首先,[[Call]]是关于Function.prototype.call()
的,所以最好先理解call()
,因为它比算法更容易。
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call
我从MDN上得到了这个示例代码,并尝试粗略地回答这个问题。 (如果你想详细了解,你和我必须了解整个规范,但我没有!)
function greet() {
var reply = [this.person, 'Is An Awesome', this.role].join(' ');
console.log(reply);
}
var obj = {
person: 'Douglas Crockford', role: 'Javascript Developer'
};
greet.call(obj); // Douglas Crockford Is An Awesome Javascript Developer
大约 2
First, what does the result of the FunctionBody calculation mean?
FunctionBody 是函数的代码。从示例中,FunctionBody 在下面。
var reply = [this.person, 'Is An Awesome', this.role].join(' ');
console.log(reply);
the result of the FunctionBody calculation
是代码执行的结果。结果用Completion Specification Type表示(下面解释)。它包括函数 return
值,但具有更广泛的信息。
What does it mean there is no [[Code]] property when this happens?
下面是空函数的意思。
function greet() {
// empty
}
And most importantly, what does this record mean (normal, undefined, empty).
是Completion Specification Type。这是规范中的值或语句的表达式。
Completion 类型的值是 (type, value, target).
形式的三元组
大约 4,5,6
What does result.type mean, result.value? Where does this value come from? Explain for each point
result为Completion Specification Type,form为(type, value, target)。
我需要帮助来理解该算法:
[[Call]]
When the [[Call]] internal method for a Function object F is called with a this value and a list of arguments, the following steps are taken:
- Let funcCtx be the result of establishing a new execution context for function code using the value of F's [[FormalParameters]] internal property, the passed arguments List args, and the this value as described in 10.4.3.
- Let result be the result of evaluating the FunctionBody that is the value of F's [[Code]] internal property. If F does not have a [[Code]] internal property or if its value is an empty FunctionBody, then result is (normal, undefined, empty).
- Exit the execution context funcCtx, restoring the previous execution context.
- If result.type is throw then throw result.value.
- If result.type is return then return result.value.
- Otherwise result.type must be normal. Return undefined.
https://www.ecma-international.org/ecma-262/5.1/#sec-13.2.1
正是我需要在 2,4,5,6 条款中的解释。
关于2:首先,FunctionBody计算的结果是什么意思?它是如何计算的?发生这种情况时没有 [[Code]] 属性 是什么意思?最重要的是,这条记录是什么意思(正常、未定义、空)。
关于4,5,6:result.type是什么意思,result.value?这个值从何而来?对每个点进行解释
P.S如果您投反对票,请解释您这样做的原因!
首先,[[Call]]是关于Function.prototype.call()
的,所以最好先理解call()
,因为它比算法更容易。
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call
我从MDN上得到了这个示例代码,并尝试粗略地回答这个问题。 (如果你想详细了解,你和我必须了解整个规范,但我没有!)
function greet() {
var reply = [this.person, 'Is An Awesome', this.role].join(' ');
console.log(reply);
}
var obj = {
person: 'Douglas Crockford', role: 'Javascript Developer'
};
greet.call(obj); // Douglas Crockford Is An Awesome Javascript Developer
大约 2
First, what does the result of the FunctionBody calculation mean?
FunctionBody 是函数的代码。从示例中,FunctionBody 在下面。
var reply = [this.person, 'Is An Awesome', this.role].join(' ');
console.log(reply);
the result of the FunctionBody calculation
是代码执行的结果。结果用Completion Specification Type表示(下面解释)。它包括函数 return
值,但具有更广泛的信息。
What does it mean there is no [[Code]] property when this happens?
下面是空函数的意思。
function greet() {
// empty
}
And most importantly, what does this record mean (normal, undefined, empty).
是Completion Specification Type。这是规范中的值或语句的表达式。 Completion 类型的值是 (type, value, target).
形式的三元组大约 4,5,6
What does result.type mean, result.value? Where does this value come from? Explain for each point
result为Completion Specification Type,form为(type, value, target)。