extends null

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

extends null

Axel Rauschmayer
Is this the best way to use `extends null`?

```js
class C extends null {
    constructor() {
        let _this = Object.create(C.prototype);
        return _this;
    }
}
```

You can’t use `this`, because it can’t be initialized via a super-constructor call: the super-constructor is `Function.prototype` (which can’t be constructor-called).

-- 
Dr. Axel Rauschmayer
[hidden email]
rauschma.de




_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: extends null

Glen Huang
Isn't super-constructor null in this case?


superConstructor is C.[[GetPrototypeOf]]()

which should be `null` after the class definition, if I'm not wrong. (But it finally throws due to type error, so technically speaking, you can't even reference the super constructor)

I can't think of a better way of extending null, but I wonder what's the use case? null usually represents lacking of a value, and extending the lacking of a value? My brain hurts.

On May 7, 2015, at 4:25 PM, Axel Rauschmayer <[hidden email]> wrote:

Is this the best way to use `extends null`?

```js
class C extends null {
    constructor() {
        let _this = Object.create(C.prototype);
        return _this;
    }
}
```

You can’t use `this`, because it can’t be initialized via a super-constructor call: the super-constructor is `Function.prototype` (which can’t be constructor-called).

-- 
Dr. Axel Rauschmayer
[hidden email]
rauschma.de



_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: extends null

Claude Pache
In reply to this post by Axel Rauschmayer

> Le 7 mai 2015 à 10:25, Axel Rauschmayer <[hidden email]> a écrit :
>
> Is this the best way to use `extends null`?
>
> ```js
> class C extends null {
>     constructor() {
>         let _this = Object.create(C.prototype);
>         return _this;
>     }
> }
> ```

No, you should say: `Object.create(new.target.prototype)`

—Claude

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: extends null

Claude Pache
In reply to this post by Glen Huang

Le 7 mai 2015 à 11:49, Glen Huang <[hidden email]> a écrit :

Isn't super-constructor null in this case?


superConstructor is C.[[GetPrototypeOf]]()

which should be `null` after the class definition, if I'm not wrong. (But it finally throws due to type error, so technically speaking, you can't even reference the super constructor)

No, as a special case of the `extends` semantics, `C.[[GetPrototypeOf]]()` will be %FunctionPrototype%; see step 6.e.ii of:

The reason is presumably that, since the constructor is a function, it should always have the methods for functions (`.bind`, `.call`, etc.) on its prototype chain.

The true meaning of `C extends null` is the following: The instances of `C` won’t have %ObjectPrototype% in their prototype chain. (For the use cases, don’t ask me.)

—Claude

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: extends null

Glen Huang
Ah, didn't notice the special case, thanks for the heads up.

Having methods for function in the prototype chain makes sense.

Another related question: If I want to override a superclass's constructor(), without calling it, I should do something like this?

```js
class A extends B {
   constructor() {
       let _this = Object.create(new.target.prototype);
       return _this;
   }
}
```

Anyway to use `this` here? Having to use a different name is a bit painful.

On May 7, 2015, at 6:55 PM, Claude Pache <[hidden email]> wrote:


Le 7 mai 2015 à 11:49, Glen Huang <[hidden email]> a écrit :

Isn't super-constructor null in this case?


superConstructor is C.[[GetPrototypeOf]]()

which should be `null` after the class definition, if I'm not wrong. (But it finally throws due to type error, so technically speaking, you can't even reference the super constructor)

No, as a special case of the `extends` semantics, `C.[[GetPrototypeOf]]()` will be %FunctionPrototype%; see step 6.e.ii of:

The reason is presumably that, since the constructor is a function, it should always have the methods for functions (`.bind`, `.call`, etc.) on its prototype chain.

The true meaning of `C extends null` is the following: The instances of `C` won’t have %ObjectPrototype% in their prototype chain. (For the use cases, don’t ask me.)

—Claude


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss