New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow calling class constructor as a function(without new) #321

Closed
disjukr opened this Issue Jan 23, 2016 · 3 comments

Comments

Projects
None yet
4 participants
@disjukr

disjukr commented Jan 23, 2016

es2015 says "If F’s [[FunctionKind]] internal slot is "classConstructor", throw a TypeError exception."
but i don't know why.

i just wanted to make abstract class decorator using babel:

function abstract(target) { // decorator
    return function cls() {
        if (new.target === cls) {
            throw new Error('instantiating abstract class is disallowed');
        }
        target.call(this, ...arguments);
    }
}

@abstract class A {
    constructor() {
        console.log('a');
    }
}
class B extends A {
    constructor() {
        super();
        console.log('b');
    }
}

// new A(); // Error: instantiating abstract class is disallowed
new B(); // intended no error but it emits TypeError

call constructor proposal seems can't solve my problem.

@meandmycode

This comment has been minimized.

Show comment
Hide comment
@meandmycode

meandmycode Jan 23, 2016

Well, B is trying to extend A which is no longer a class after calling abstract but a function.. if you really want to do this, you could do the following:

function abstract(target) {
    return class cls extends target {

      constructor(...args) {

        if (new.target === cls) {
            throw new Error('instantiating abstract class is disallowed');
        }

        super(...args);

      }

    }
}

meandmycode commented Jan 23, 2016

Well, B is trying to extend A which is no longer a class after calling abstract but a function.. if you really want to do this, you could do the following:

function abstract(target) {
    return class cls extends target {

      constructor(...args) {

        if (new.target === cls) {
            throw new Error('instantiating abstract class is disallowed');
        }

        super(...args);

      }

    }
}
@zenparsing

This comment has been minimized.

Show comment
Hide comment
@zenparsing

zenparsing Jan 23, 2016

Contributor

This question is not appropriate for this repository. Please move this to the es-dicuss mailing list (and try looking at the archives as well).

Contributor

zenparsing commented Jan 23, 2016

This question is not appropriate for this repository. Please move this to the es-dicuss mailing list (and try looking at the archives as well).

@domenic

This comment has been minimized.

Show comment
Hide comment
@domenic

domenic Jan 23, 2016

Member

Yes, please ask help questions on Stack Overflow or es-discuss. See https://github.com/tc39/ecma262/blob/master/CONTRIBUTING.md for what is acceptable to post to this repository.

Member

domenic commented Jan 23, 2016

Yes, please ask help questions on Stack Overflow or es-discuss. See https://github.com/tc39/ecma262/blob/master/CONTRIBUTING.md for what is acceptable to post to this repository.

@domenic domenic closed this Jan 23, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment