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?