why creators of the TypeScript compiler didn’t turn the class into the JavaScript closure that makes variables private

Viewed 159

Why typescript compiler left to convert classes into closure to achieve data hiding ?

class Person {
    public name: string;     
    private password: string;

    constructor(name:string,password: string) {
        this.name = name;       
        this.password= password;
    }
} 
let p = new Person("mihir","!@#123");

In above code I kept password as private variable.So we should not access that variable directly. following code is compiled from typescript code . password variable remains public as we don't have access modifier in javascript.

var Person = (function () {
    function Person(name,password) {
        this.name= name;
        this.password= password;
    }
    return Person;
})();

var p = new Person("mihir","!@#123");

As per the following code using closure variable can be protected from outside .

var Person = (function () {
    var _pass;     
    function Person(name,password) {       
        this.name = name;        
        _pass = password;
    }
    return Person;
})();   

We understand that data encapsulation is applicable in typescript,and Typescript's goal is to write more productive code than javascript.

Then why typescript left converting code to closure? why it left data hiding at compiled code as it can be implemented by closure.

At the end without javacsript there can not be typescript. So was there complexity for compiler to achieve data hiding at compiled code?

1 Answers

I have found some things which i guess that stopped thinking to typescript compiler creators to turn the class to javascript closure to make private variable.

1)If we use following code to keep variable private

var Person = (function () {
    var _pass;     
    function Person(name,password) {       
        this.name = name;        
        _pass = password;
    }

    Person.prototype.getPassword = function() {
        return _pass;
    }

    return Person;
})();

var p1 = new Person("mihir","!@#123");
var p2 = new Person("khushbu","!@#345");
console.log(p1.getPassword()); // '!@#345'
console.log(p2.getPassword()); // '!@#345'

In above example _pass behaves as static.since _pass is not assigned to constructor , whenever creating new object, new object's password overrides the old object's password .So that works only for single object.And that's the worst practice.

2) Move everything into constructor

var Person = (function () {

    function Person(name, password) {       
        var _pass = password;
        this.name = name;
        this.getPassword = function () {
            return _pass;
        }
    }
    return Person;
})();

var p1 = new Person('mihir','!@#123');
var p2 = new Person('khushbu','!@#456');
console.log(p1.getPassword()); // '!@#123'
console.log(p2.getPassword()); // '!@#456'

But this is again bad idea to move everything into constructor.which result into performance degradation.So better not to implement private variable scope at javascript.

So to make use prototype pattern typescript compiler creators left the idea of implementing private scope.

Every javascript developer should make use of underscore prefix to "private" variables notifying to consumer of your code that 'use at your own risk'. And make a full stop to use a private variable in javascript.

Related