简体   繁体   English

JS ES5 OOP:通过构造函数中的get / set的私有实例成员

[英]JS ES5 OOP: private instance member via get/set in constructor

I'm trying to create a constructor in a namespace (IIFE module pattern) that has a private instance member of sort that can only be changed via set/get methods. 我试图在名称空间(IIFE模块模式)中创建一个构造函数,该构造函数具有只能通过set / get方法更改的私有实例成员 I use an IIFE that returns get/set functions that create a closure on the data, and return the get/set to the instance property. 我使用IIFE,该IIFE返回获取/设置函数,该函数在数据上创建一个闭包,并将获取/设置返回到实例属性。 From my testing the pattern seems to work; 从我的测试来看,这种模式似乎有效; meaning the members are private and can be accessed through the get/set only. 表示成员是私有的,只能通过get / set进行访问。

A) do you think this is true? A)你认为这是真的吗?

B) Are these good patterns or are there better alternatives (in ES5)? B)这些是好的模式还是有更好的替代方法(在ES5中)?

Structure and Fiddle: 结构和提琴:

IIFE module -> constructor-> IIFE that returns get/set

Fiddle 小提琴

Thanks ahead of time 提前谢谢

//namespace 
var computerModule = (function() {
  //function constructor 
  function Computer(CPUMemory, diskMemory, CPUModel, price, warrantyInYears) {
    this.CPUMemory = CPUMemory;
    this.diskMemory = diskMemory;
    this.CPUModel = CPUModel;
  }
  Computer.prototype.buyAccessories = buyAccessories;
  Computer.prototype.print = print;
  Computer.prototype.toString = toString;

  function buyAccessories() {

  }

  function print() {
    console.log(this.toString());
  }

  function toString() {
    return `CPUMemory: ${this.CPUMemory},diskMemory: ${this.diskMemory}, CPUModel: ${this.CPUModel}, price: ${price}, warrantyInYears: ${warrantyInYears}`;
  }

  //return constructor
  return {
    Computer
  }
})();

//namespace 
var laptopComputerModule = (function() {
  //constructor
  function Laptop(CPUMemory, diskMemory, CPUModel, price, warrantyInYears, ChargingTime, ) {

    computerModule.Computer.call(this, CPUMemory, diskMemory, CPUModel, price,
      warrantyInYears);

    //trying to create a member that cannot be directly changed - need feeback
    Object.defineProperty(this, "BatteryChargingTime", {
      value: (function() {
        //private variable
        var BatteryChargingTime;
        //constructor init
        BatteryChargingTime = ChargingTime;

        function get() {
          return BatteryChargingTime;
        }

        function set(arg) {
          BatteryChargingTime = arg;
        }

        return { //functions create closuers on variables in this function, keeping its scope intact
          get,
          set
        }
      }()),
      writable: false,
      configurable: true
    })
  }
}());

There's no need to put an IIFE inside the constructor. 无需在构造函数中放置IIFE。 The constructor function already provides a scope. 构造function已经提供了作用域。

function Laptop(CPUMemory, diskMemory, CPUModel, price, warrantyInYears, ChargingTime, ) {
  computerModule.Computer.call(this, CPUMemory, diskMemory, CPUModel, price, warrantyInYears);

  this.BatteryChargingTime = {
    get: function() {
      return ChargingTime;
    },
    set: function(arg) {
      ChargingTime = arg;
    }
  }
}

Apart from that, it's a totally standard pattern. 除此之外,这是一个完全标准的模式。 (Of course there's no point in making a "private" member that can be arbitrarily set by its setter method). (当然,创建可以通过其setter方法任意设置的“私有”成员没有意义)。

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

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