Question

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:

ToLength

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:

ReturnIfAbrupt

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!

Was it helpful?

Solution

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.

OTHER TIPS

According to the ECMAScript Spec.

The term “abrupt completion” refers to any completion with a [[type]] value other than normal.

A Completion Record is a "object" with three data member: type ,value and target.

And http://people.mozilla.org/~jorendorff/es6-draft.html#sec-normalcompletion is a example of Completion Record.

Still, it is not clear enoughXD , maybe you need further reading on the reading.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top