Recently, I read the ECMAScript Language Specification. I didn't plan to read the whole specification, I just picked up some parts. I came cross many questions, one of them is like this:
1.Let len be ToInteger(argument).
2.ReturnIfAbrupt(len).
3.If len ≤ +0, then return +0.
4.Return min(len, 2^53-1).
As I understand, it should be like this:
var len = ToInteger(argument); // step 1
len = ReturnIfAbrupt(len);// step 2
// step 3
if(len<=0){
return +0; //-0 is OK too?
}
return Math.min(len, Math.pow(2,53)-1); // step 4
I didn't understand what the meaning of ReturnIfAbrupt(len) is, and I found this:
1.If argument is an abrupt completion, then return argument.
2.Else if argument is a Completion Record, then let argument be argument.[[value]].
What is abrupt completion, and the differents between it and Completion Record?Can they combine one step:If argument is an Completion Record, then return argument.Any suggestion will be gratefull!
My understanding is that in ES 6, when an expression is evaluated, it returns a completion record (which is a specification device for explaining behaviour) that holds information about the result of evaluating the expression.
In the case above, if the argument is an abrupt completion record (i.e. its type is anything other than normal), then it is returned.
If the argument is not an abrupt completion record, its value is returned.
Which essentially means that if the conversion of the argument to an integer went OK, return the value. Otherwise, propigate an error value.