亲爱的社区和Javascript向导,模块模式和对象实例 - 属性值的意外更改
我遇到了一个我无法理解的行为。
我的意图是创建一个模块,它模拟私有变量并控制对象的创建。
我希望下面的例子说明我想要什么,我的问题是什么。
const ex = (() => {
// Creating the constructor for points
function Point() {
let x;
let y;
// I think using prototype is a good idea, because it can be expected that a lot of points are created. So I do not waste memory with a lot of function duplications.
Point.prototype.setX = xValue => {
x = xValue;
}
Point.prototype.setY = yValue => {
y = yValue;
}
Point.prototype.getY =() => {
return y;
}
}
// Returning an interface to create points
// but hide their properties.
return {createPoint: (x,y) => {
let point = new Point();
point.setX(x);
point.setY(y);
return point;
}}
})();
p1 = ex.createPoint(1,2);
console.log(p1.getY()); // Result is '2' as expected
p2 = ex.createPoint(3,4);
console.log(p2.getY()); // Result is '4' as expected
console.log(p1.getY()); // Result is '4'..Wait...'4' what? Why?!
我觉得明显的来源是我的理解范围湖... 我的假设是/是,如果我创建一个函数作为原型属性: 1。函数用于创建类型的所有对象都可见。 2.原型函数在正在使用它的对象的作用域上运行。
从我的结果中,我怀疑数字2是正确的。
所以我给它一个镜头,并直接分配的属性和方法到新创建的对象(我希望我这样做了......)
const ex = (() => {
// Creating the constructor for points
function Point() {
this.x;
this.y;
// I think using prototype is a good idea, because it can be expected that a lot of points are created. So I do not waste memory with a lot of function duplications.
this.setX = xValue => {
x = xValue;
}
this.setY = yValue => {
y = yValue;
}
this.getY =() => {
return y;
}
}
// Returning an interface to create points
// but hide their properties.
return {createPoint: (x,y) => {
let point = new Point();
point.setX(x);
point.setY(y);
return point;
}}
})();
p1 = ex.createPoint(1,2);
console.log(p1.getY()); // Result is '2' as expected
p2 = ex.createPoint(3,4);
console.log(p2.getY()); // Result is '4' as expected
console.log(p1.getY()); // Result is '4'..Wait...'4' what? Why?!
但结果却事与愿违改变,我无法理解行为。 在重读关于我的js-bible中的闭包和原型的章节后,我不知道在哪里可以搜索或找到帮助,而不是问你。
如果你能指出我的错误并解释我的代码出了什么问题,我会很高兴。
亲切的问候
吉姆
亲爱比伦特·武拉尔,非常感谢你为你的回答。关于箭头函数的提示是一个很好的帮助和一个新的有价值的见解(=你提出的解决方案毫无疑问是一种改进,但是其中一个特性(以前曾为一个对象工作过)丢失了。现在x和y不再隐藏例如,可以直接用p1.x访问它们(我很抱歉,因为没有在代码中做这个明确的说明)因此,我尝试了第一次尝试'let x',以便它们隐藏在范围中。 – Jim
我用一些更多的信息更新了我的答案,有很多方法可以让真正的私人成员,但它真的不值得,我反正举了一个例子。 –
非常感谢你!你的回答非常有帮助,是一个很好的观点我可以继续工作(= – Jim