Understanding Intl constructors in ECMA-402 2nd edition

18 views
Skip to first unread message

Andy VanWagoner

unread,
Jul 10, 2015, 2:43:49 PM7/10/15
to javascript-g...@googlegroups.com
I'm trying to implement the Intl APIs in WebKit. I had the constructors mostly complete when I saw the new edition of the spec. I'd like to make sure I understand the implications of the changes, since the terminology is updated to match the ES 2015 spec, and not all of the concepts therein are concrete in WebKit's codebase yet.

In the 1.0 spec, calling a constructor with an existing object would initialize the object as a Collator (or NumberFormat or DateFormat) rather than creating a new object.

var obj = {}
Intl.Collator.call(obj) // returns obj

2.0 specifies the following:

1. If NewTarget is undefined, let newTarget be the active function object, else let newTarget be NewTarget.
2. Let collator be OrdinaryCreateFromConstructor(newTarget, %CollatorPrototype%).
3. ReturnIfAbrupt(collator).
4. Return InitializeCollator(collator, locales, options).

If I'm understanding correctly, that means that a new object is always created, and that new object's prototype is newTarget.prototype || 
%CollatorPrototype%. So when a class that extends Collator calls super() in the constructor, the new object is constructed with that class's prototype.

let obj = {}
Intl.Collator.call(obj) // no newTarget, so equivalent to new Intl.Collator()
class Collator2 extends Intl.Collator {
  constructor (locale, options) {
    super(locale, options)
    this.extra = 'foo'
  }
}
Collator2.prototype instanceof Intl.Collator ?
new Collator2() instanceof Intl.Collator // true
new Collator2().compare() // 0

Does that sound right?

Rick Waldron

unread,
Jul 10, 2015, 3:11:19 PM7/10/15
to Andy VanWagoner, javascript-g...@googlegroups.com
On Fri, Jul 10, 2015 at 2:43 PM Andy VanWagoner <thetale...@gmail.com> wrote:
I'm trying to implement the Intl APIs in WebKit. I had the constructors mostly complete when I saw the new edition of the spec. I'd like to make sure I understand the implications of the changes, since the terminology is updated to match the ES 2015 spec, and not all of the concepts therein are concrete in WebKit's codebase yet.

In the 1.0 spec, calling a constructor with an existing object would initialize the object as a Collator (or NumberFormat or DateFormat) rather than creating a new object.

var obj = {}
Intl.Collator.call(obj) // returns obj

2.0 specifies the following:

1. If NewTarget is undefined, let newTarget be the active function object, else let newTarget be NewTarget.
2. Let collator be OrdinaryCreateFromConstructor(newTarget, %CollatorPrototype%).
3. ReturnIfAbrupt(collator).
4. Return InitializeCollator(collator, locales, options).

If I'm understanding correctly, that means that a new object is always created,

Correct.
 
and that new object's prototype is newTarget.prototype || 
%CollatorPrototype%. So when a class that extends Collator calls super() in the constructor, the new object is constructed with that class's prototype.

Correct. 
 

let obj = {}
Intl.Collator.call(obj) // no newTarget, so equivalent to new Intl.Collator()
class Collator2 extends Intl.Collator {
  constructor (locale, options) {
    super(locale, options)
    this.extra = 'foo'
  }
}
Collator2.prototype instanceof Intl.Collator ?
new Collator2() instanceof Intl.Collator // true
new Collator2().compare() // 0

Does that sound right?

Yes, this is exactly right. 

Rick

Andy VanWagoner

unread,
Jul 10, 2015, 5:41:15 PM7/10/15
to javascript-g...@googlegroups.com, thetale...@gmail.com
Thanks!
Reply all
Reply to author
Forward
0 new messages