简体   繁体   English

为什么TypeScript编译器的创建者没有将类转换为使变量变为私有的JavaScript闭包

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

Why typescript compiler left to convert classes into closure to achieve data hiding ? 为什么typescript编译器会将类转换为闭包来实现数据隐藏?

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 . 以下代码是从typescript代码编译的。 password variable remains public as we don't have access modifier in javascript. 密码变量保持公开,因为我们在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. 我们知道数据封装适用于打字稿,而Typescript的目标是编写比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. 最后没有javacsript就没有打字稿。 So was there complexity for compiler to achieve data hiding at compiled code? 那么编译器在编译代码中实现数据隐藏的复杂性是什么?

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. 我发现了一些我认为停止思考打字稿编译器创建者将这个类转换为javascript闭包来制作私有变量的东西。

1)If we use following code to keep variable private 1)如果我们使用以下代码来保持变量私有

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. 在上面的示例中,_pass表现为static.since _pass未分配给构造函数,每当创建新对象时,新对象的密码都会覆盖旧对象的密码。这样只适用于单个对象。这是最糟糕的做法。

2) Move everything into constructor 2)将所有内容移动到构造函数中

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. 但是将一切都移到构造函数中也是一个坏主意。这会导致性能下降。最好不要在javascript上实现私有变量作用域。

So to make use prototype pattern typescript compiler creators left the idea of implementing private scope. 因此,要使用原型模式typescript编译器创建者离开了实现私有范围的想法。

Every javascript developer should make use of underscore prefix to "private" variables notifying to consumer of your code that 'use at your own risk'. 每个javascript开发人员都应该使用下划线前缀来“私有”变量,通知消费者您的代码“使用风险自负”。 And make a full stop to use a private variable in javascript. 并完全停止在javascript中使用私有变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM