我一直在阅读 Diaz 的书 Pro JavaScript Design Patterns。很棒的书。我本人无论如何都不是专业人士。我的问题:我可以拥有一个可以访问私有实例变量的静态函数吗?我的程序有一堆设备,其中一个的输出可以连接到另一个的输入。此信息存储在输入和输出数组中。这是我的代码:
var Device = function(newName) {
var name = newName;
var inputs = new Array();
var outputs = new Array();
this.getName() {
return name;
}
};
Device.connect = function(outputDevice, inputDevice) {
outputDevice.outputs.push(inputDevice);
inputDevice.inputs.push(outputDevice);
};
//implementation
var a = new Device('a');
var b = new Device('b');
Device.connect(a, b);
这似乎不起作用,因为 Device.connect 无权访问设备输出和输入数组。有没有办法在不向设备添加会暴露它的特权方法(如 pushToOutputs)的情况下获取它们?
谢谢!史蒂夫。