繁体   English   中英

JavaScript私有实例变量?

[英]Javascript private instance variables?

参考Javascript:Good部分,试图建立一种类原型对象,然后我可以用它来创建实例。 但是使用建议的用于通过闭包创建对象和信息隐藏的模式,我发现我创建了一个私有静态成员而不是私有实例成员。

在这种情况下,我尝试创建一个“帐户”对象,该对象具有一个由balance()方法返回的私有值(余额)变量:

<head>
    <script>
        if (typeof Object.create !== 'function') {
            Object.create = function (o) {
                var F = function () {
                };
                F.prototype = o;
                return new F;
            };
        };
        var account = function() {
            var value = 0;
            return {
                "account_name": "",
                deposit: function (amount) {
                    value = value + amount;
                },
                withdrawal: function (amount) {
                    value = value - amount;
                },
                balance: function( ) {
                    return value;
                }
            }
        }();
    </script>
</head>
<body>
    <script>
        var account1 = Object.create(account);
        var account2 = Object.create(account);
        account1.account_name = "Mario Incandenza";
        account1.deposit(100);
        account1.withdrawal(25);
        account2.account_name = "Hal Incandenza";
        account2.deposit(5100);
        account2.withdrawal(2000);
        document.writeln("<p>Account name = " + account1.account_name + "  Balance: " + account1.balance() + "</p>");
        document.writeln("<p>Account name = " + account2.account_name + "  Balance: " + account2.balance() + "</p>");
    </script>
</body>

结果如下:

帐户名称= Mario Incandenza余额:3175

帐户名称= Hal Incandenza余额:3175

因此,看到两个账户的所有提款和存款的总和,很明显,我已经创建了一个静态的“类”变量(在javaspeak中)

那么,如何在实例级别创建var值并使其保持隐藏/私有状态呢?

var account =声明末尾的()表示您仅调用一次初始化函数,并将其返回值分配给您命名为account的单个对象。 然后,您将使用单个对象作为原型制作多个对象,这意味着它们都使用同一个闭包且具有单个余额值。

您要做的是使该函数account化,并为每个新对象分别调用它。 您只需移动括号即可:

var account = function() {
    var value = 0;
    return {
        account_name: "",
        deposit: function (amount) {
            value = value + amount;
        },
        withdrawal: function (amount) {
            value = value - amount;
        },
        balance: function( ) {
            return value;
        }
    }
};

var account1 = Object.create(account());
var account2 = Object.create(account());

但是,一旦您将功能设置为account ,就没有理由使用Object.create 您可以直接使用函数的返回值:

var account1 = account();
var account2 = account();

或者,您可以使用new方式以更传统的方式进行操作(即使克罗克福德说new是邪恶的):

var Account = function() {
  var value = 0;
  this.deposit = function (amount) {
    value = value + amount;
  };
  this.withdrawal = function (amount) {
    value = value - amount;
  };
  this.balance = function( ) {
    return value;
  }
}

var account1 = new Account();
var account2 = new Account();

那仍然不是很传统的经典Javascript,只能在Account.prototype上定义一次方法。 为了完成闭包封装,此版本必须在每个实例上创建方法的新副本。

实际上,最好编写类似以下内容的代码:

var account = function(/** name, id **/) {
  this.value = 0;
  //this.name = name;
  //this.id = id;
}

account.prototype.deposit = function(amount) {
  this.value += amount;
}

...

您将为每个实例返回函数,因此与原型相比,它花费更多的内存。

暂无
暂无

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

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