简体   繁体   English

使用Javascript时如何更新保留值的值?

[英]How do I update the value remains in closure when using Javascript?

It might be a very basic question, but I'm so new to Javascript, I don't even know which keyword I should search about to find this. 这可能是一个非常基本的问题,但是我对Javascript还是很陌生,我什至不知道我应该搜索哪个关键字才能找到它。 If this question has already been asked, I'm so sorry and please let me know which question it is. 如果已经问过这个问题,对不起,请告诉我是哪个问题。

I have read that getter and setter can't be declared at prototype, so I tried to test what happens if I declare getter and setter in prototype. 我读过getter和setter不能在原型中声明,所以我尝试测试如果在原型中声明getter和setter会发生什么。 But I faced another problem. 但是我面临另一个问题。

This is a simple code that declares a constructor to make object 'Rectangle', it includes getter and setter. 这是一个简单的代码,它声明一个构造函数以使对象成为“ Rectangle”,它包括getter和setter。

But when I run this code, I get '35' as a result of running getArea(), although I changed the value of width to 10 by using setWidth(); 但是,当我运行此代码时,尽管我通过使用setWidth()将width的值更改为10,但由于运行getArea()却得到了'35'。 I checked the value of width on console but it says your width is changed to 10. 我在控制台上检查了width的值,但它说您的宽度已更改为10。

So I guess this is something about closure, getArea() loads width and height when it is declared and it has not been updated as the value I set. 所以我想这与闭包有关,getArea()在声明时加载宽度和高度,并且尚未将其更新为我设置的值。

1) How can I update value in getArea() when I change the value of height and width by setter? 1)当我通过设置器更改高度和宽度的值时,如何更新getArea()中的值? (How can I calculate area by using functions with updated value? Of course I still want to encapsulate it. So width and height would not be this.width and this.height) (如何使用具有更新值的函数来计算面积?当然我仍然想封装面积。因此,宽度和高度将不是this.width和this.height)

2) If my gut feeling about closure is wrong, why getArea() keeps spitting 35 on my face? 2)如果我对封闭的直觉是错误的,为什么getArea()会在我的脸上不断吐出35?

3) What I wanted to try first, why can't I declare getter and setter in prototype? 3)我想首先尝试什么,为什么不能在原型中声明getter和setter?

function Rectangle(w, h) {
var width = w;
var height = h;

this.getWidth = function() {
    return w;
};
this.getHeight = function() {
    return h;
};
this.setWidth = function(w) {
    if (w < 0) {
        throw 'width cannot be under 0';
    } else {
        console.log('width = w');
        width = w;
        console.log('I put : ' + w);
        console.log('width = ' + width);
    }
};

this.setHeight = function(h) {
    if (h < 0) {
        throw 'height cannot be under 0';
    } else {
        console.log('height = h');
        height = h;
    }
};
}
Rectangle.prototype.getArea = function() {
    return this.getWidth() * this.getHeight();
};

var rectangle = new Rectangle(5, 7);
rectangle.setWidth(10);

alert('AREA : ' + rectangle.getArea());

You have to return 'Width' instead of 'w'. 您必须返回“宽度”而不是“ w”。 Here I have altered you code check this, 在这里,我更改了您的代码,请检查此内容,

       this.getWidth = function () {
            return width;
        };
        this.getHeight = function () {
            return height;
        };

  function Rectangle(w, h) { var width = w; var height = h; this.getWidth = function () { return width; }; this.getHeight = function () { return height; }; this.setWidth = function (w) { if (w < 0) { throw 'width cannot be under 0'; } else { console.log('width = w'); width = w; console.log('I put : ' + w); console.log('width = ' + width); } }; this.setHeight = function (h) { if (h < 0) { throw 'height cannot be under 0'; } else { console.log('height = h'); height = h; } }; } Rectangle.prototype.getArea = function () { return this.getWidth() * this.getHeight(); }; var rectangle = new Rectangle(5, 7); rectangle.setWidth(10); alert('AREA : ' + rectangle.getArea()); 

Your getters are returning the w and h you've passed in, instead of the local closure's width and height variables. 您的吸气剂将返回您传入的wh ,而不是局部闭包的widthheight变量。 You then set this width and height in your setters, but the original w and h remain their initial values and are returned as such in your getters. 然后,您可以在设置器中设置此widthheight ,但是原始的wh保持其初始值,并在您的吸气器中返回。

Try updating your two getter functions to return your width and height if you want them to return the updated values you use in your setters: 如果希望它们返回设置程序中使用的更新值,请尝试更新两个getter函数以返回widthheight

this.getWidth = function() {
  return width;
};
this.getHeight = function() {
  return height ;
};

暂无
暂无

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

相关问题 如何更新内部函数(闭包)中的值,使其始终保持更新直到更改(JavaScript 闭包概念) - How to update value in inner function (closure) so that it remains updated throughout until changed (JavaScript Closure Concept) 如何重用此JavaScript超时关闭? - How do I reuse this JavaScript timeout closure? 如何阅读 Javascript 闭包语法? - How do I read Javascript Closure Syntax? 使用 Javascript SVG 画线时,多个<svg>是在鼠标移动时生成的。 我如何确保只有最后一个<svg>遗迹?</svg></svg> - When using Javascript SVG to draw a line, multiple <SVG>'s are made as the mouse moves. How do I ensure only the last <SVG> remains? 如何使用 onchange 事件更新输入值并获取 vanilla Javascript 中的值? - How do I update the input value using an onchange event and get the value in vanilla Javascript? 我需要Javascript关闭吗? - Do I need a Javascript Closure? 在使用 Google 的 Closure 转译 JavaScript 时,如何使用 class 作为名称空间? - How do you use a class as a name space, when transpiling JavaScript using Google's Closure? Javascript关闭没有使用正确的值 - Javascript closure not using correct value Javascript - 使用参数引用全局函数的匿名函数中的闭包 - 我如何使用preventDefault? - Javascript - closure in anonymous function referencing a global function using arguments - how do i use preventDefault? 如何使用 Google 的 Closure Compiler 将我的 javascript 拆分为模块? - How do I split my javascript into modules using Google's Closure Compiler?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM