By specs, a new instance is created and passed as the `thisObject' for the
constructor function anytime that function is called with the `new'
operator. It could be optimised as long as that object is not used.

---
function Point(x, y) {
    this.x = x
    this.y = y
    console.log(this)
    return { x: x, y: y }
}
new Point(1, 1) instanceof Point
// => false
---

This can't be optimised because the `this' object is used. A more real-world
use case would be, however:

---
function Point(x, y) {
    if (!(this instanceof Point))  return new Point(x, y)
    this.x = x
    this.y = y
}
Point(1, 2) instanceof Point
// => true
new Point(1, 2) instanceof Point
// => true
---

Not sure how engines optimise that particular case, I'm not much familiar
with JIT techniques either :3

2011/10/10 Axel Rauschmayer <[email protected]>

> Right. Does that prevent an instance from being created? But it shouldn’t
> be hard to let a compiler perform that kind of optimization.
>
> On Oct 10, 2011, at 13:58 , Quildreen Motta wrote:
>
> We already get factory constructors by default -- by returning an object
> from the constructor function.
>
> 2011/10/10 Axel Rauschmayer <[email protected]>
>
>> In many ways, Dart feels *less* versatile than ECMAScript.next. However,
>> there are two ideas that would make a nice addition to class literals:
>>
>> - Named constructors: Without overloading, you need a way to distinguish
>> constructors:
>>    new Point.zero()
>> - Factory constructors: are a way to mark constructors so that no instance
>> is produced automatically. Then you can return cached instances or instances
>> of a subclasses.
>>
>> Details: http://www.dartlang.org/articles/idiomatic-dart/
>>
>> --
>> Dr. Axel Rauschmayer
>>
>> [email protected]
>> twitter.com/rauschma
>>
>> home: rauschma.de
>> blog: 2ality.com
>>
>>
>>
>> _______________________________________________
>> es-discuss mailing list
>> [email protected]
>> https://mail.mozilla.org/listinfo/es-discuss
>>
>
>
> --
> Dr. Axel Rauschmayer
>
> [email protected]
> twitter.com/rauschma
>
> home: rauschma.de
> blog: 2ality.com
>
>
>
>
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to