Skip to content
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

BUG : compiler Anonymous class #18515

Closed
srburton opened this issue Sep 15, 2017 · 5 comments
Closed

BUG : compiler Anonymous class #18515

srburton opened this issue Sep 15, 2017 · 5 comments
Labels
Duplicate An existing issue was already created

Comments

@srburton
Copy link

Is Bug in compiler ?

class Carro {

    public marcas() {
        // Anonymous class, and not working  "return new class{....}"  or "return class{....}"
        return class {

            private stars: Array<number>;

            public ford(stars: number) {
                this.stars.push(stars);
                return this;
            }
            public mercedes(stars: number) {
                this.stars.push(stars);
                return this;
            }
        }
    }
}
var a = new Carro();
var marcas = a.marcas();
var ab = new marcas();
ab.ford(3);
ab.mercedes(2);

Output compile:

var Carro = (function () {
    function Carro() {
    }
    Carro.prototype.marcas = function () {
        return (function () {
            function class_1() {
            }
            class_1.prototype.ford = function (stars) {
                this.stars.push(stars);
                return this;
            };
            class_1.prototype.mercedes = function (stars) {
                this.stars.push(stars);
                return this;
            };
            return class_1;
        }());
    };
    return Carro;
}());
var a = new Carro();
var marcas = a.marcas();
var ab = new marcas();
ab.ford(3);
ab.mercedes(2);

Error:

Uncaught TypeError: Cannot read property 'push' of undefined at class_1.ford (<anonymous>:9:27) at <anonymous>:4:4

@srburton
Copy link
Author

Perfect compiler would be

var Carro = (function () {
    function Carro() {
    }
    Carro.prototype.marcas = function () {
        return (function () {
            function class_1() {
            }

            class_1.prototype.stars = [];  /// COMPILE ADD <---------------------------------------------

            class_1.prototype.ford = function (stars) {
                this.stars.push(stars);
                return this;
            };
            class_1.prototype.mercedes = function (stars) {
                this.stars.push(stars);
                return this;
            };
            return class_1;
        }());
    };
    return Carro;
}());
var a = new Carro();
var marcas = a.marcas();
var ab = new marcas();
ab.ford(3);
ab.mercedes(2);

@mhegazy
Copy link
Contributor

mhegazy commented Sep 15, 2017

Duplicate of #8476

@mhegazy mhegazy marked this as a duplicate of #8476 Sep 15, 2017
@mhegazy mhegazy added the Duplicate An existing issue was already created label Sep 15, 2017
@srburton
Copy link
Author

srburton commented Sep 15, 2017

@mhegazy This report is not duplicated because the compiler constructs the methods and forgets the attributes

screenshot_5

@RyanCavanaugh
Copy link
Member

@renatoprogramer arrays are not automatically initialized

@kitsonk
Copy link
Contributor

kitsonk commented Sep 17, 2017

Especially adding a non-primative to a prototype is not a good thing. ALL instances would share that non-primative instance, unless they initialised it in their constructor:

var Carro = (function () {
    function Carro() {
    }
    Carro.prototype.marcas = function () {
        return (function () {
            function class_1() {
            }

            class_1.prototype.stars = [];  /// COMPILE ADD <---------------------------------------------

            class_1.prototype.ford = function (stars) {
                this.stars.push(stars);
                return this;
            };
            class_1.prototype.mercedes = function (stars) {
                this.stars.push(stars);
                return this;
            };
            return class_1;
        }());
    };
    return Carro;
}());
var a = new Carro();
var marcas = a.marcas();
var ab = new marcas();
ab.ford(3);
ab.mercedes(2);
var ab2 = new marcas();
console.log(ab.stars === ab2.stars); // true!!!! Not good!
console.log(ab.stars); // [3, 2]
console.log(ab2.stars); // [3, 2]!!!!!  Not good!

As Mohammed says, initialisers are optional (and for good reasons), detecting accessing an uninitialised property would be useful and is tracked by the issue mentioned.

@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants