如何更改JavaScript的初始值

时间:2018-08-25 14:35:06

标签: javascript

例如,当我单击“减号”单选按钮并输入值1000时,初始值为10,000,初始值将变为9000。单击“添加”单选按钮并输入值2000 当前的初始字母9000 + 2000等于11,000。

问题是initialValue返回到原始值10,000。

{"x-real-ip":"xx.xxx.x.xxx","x-forwarded-for":"xx.xxx.x.xxx","host":"xxxxxxxxxxxxxxxx.localtunnel.me","x-forwarded-proto":"https","x-nginx-proxy":"true","connection":"close","content-length":"79","content-type":"application/json;charset=UTF-8","google-assistant-api-version":"v1","authorization":"Bearer aIxaA9F2N9hn_aVAgVBSb2rcrpl1m8e5","user-agent":"Mozilla/5.0 (compatible; Google-Cloud-Functions/2.1; +http://www.google.com/bot.html)","accept-encoding":"gzip,deflate,br"}
//external script
function Compute(initialNum, numOne) {
    this._initialNum = initialNum;
    this._numOne = numOne;

    this.addNum = function() {
        alert(this._initialNum = this._initialNum + this._numOne);
        return this._initialNum;
    };

    this.minusNum = function() {
        alert(this._initialNum = this._initialNum - this._numOne);
        return this._initialNum;
    };
}

//body script
var rdoAdd = document.getElementById("rdoAdd");
var rdoMinus = document.getElementById("rdoMinus");
var tblResult = document.getElementById("tblResult");

function printResult() {
    var initialValue = 10000, deposit = 0, withdraws = 0;
    var rdoAdd = document.getElementById("rdoAdd");
    var rdoMinus = document.getElementById("rdoMinus");
    var numOne = parseInt(document.getElementById('txtNumOne').value);
    var objAccount = new Compute(initialValue, numOne);

    if (rdoAdd.checked) {
        deposit += objAccount.addNum(); 
        display = "<tr>";
    	display += "<td>" + deposit + "</td>";
    	display += "<tr>";
        tblResult.innerHTML += display;

        resetx();

    } else {
        withdraws += objAccount.minusNum();
        display = "<tr>";
    	display += "<td>" + withdraws + "</td>";
    	display += "<tr>";
        tblResult.innerHTML += display;

        resetx();
    }
}

function resetx() {
    document.getElementById('txtNumOne').value = "";
    document.getElementById("rdoAdd").checked = false;
    document.getElementById("rdoMinus").checked = false;
}

4 个答案:

答案 0 :(得分:0)

只需移动

 var initialValue = 10000;
 var objAccount = new Compute(initialValue);

printResult函数之外。

答案 1 :(得分:0)

这只是与范围相关的问题。如果您在函数内部使用var,它将在本地作用域中重新声明或创建initialValue的新实例。

如果要更改变量initialValue的值,请不要使用var。这只会更改在全局范围 ...

中定义的变量

之所以有效,是因为JavaScript解释器首先检查本地范围内是否存在给定变量的实例。如果没有,它将检查全局范围内变量的存在。

编辑:

根据您的问题,您希望initialValue的值在每次计算后保持不变。为此,必须首先在objAccount函数外部的全局范围中声明对象printResult();因为在代码中,每次调用函数printValue()时,都会创建一个ComputeobjAccount)的新实例。其次,您应该从numOne类中删除Compute属性,而应将numOne作为参数传递给addNum()minusNum()

您可以将Compute的方法修改为:

this.addNum = function(_numOne) {
    ...
}

this.minusNum = function(_numOne) {
    ...
}

在调用方法时,

objAccount.addNum(<some_number>);

objAccount.minusNum(<some_number>);

此后,您甚至无需使用depositwithdraws ...

if (rdoAdd.checked) {
    objAccount.minusNum(numOne);    // This will directly adjust `initialValue`
    display = "<tr>";
    display += "<td>" + objAccount._initialNum + "</td>";
    display += "<tr>";
    tblResult.innerHTML += display;

    resetx();
}

类似地,对else语句执行相同的操作...

奖金::您似乎使用的是ES5样式,而不是使用ES6(包括类似classlet的关键字:类似于{ {1}})

这是一个代码段(使用var

ES6 classes

答案 2 :(得分:0)

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <input type="radio" id="rdoAdd" name="rdo" required>Add<br><br>
    <input type="radio" id="rdoMinus" name="rdo" reuired>Minus<br><br>
    <input type="text" id="txtNumOne"><br><br>

    <button onclick="printResult()">Compute</button><br><br>

    <table border="1px">
    <th>Result</th>

    <tbody id = "tblResult">

    </tbody>
    </table>
<script src = "java.js"></script>
<script>
    var rdoAdd = document.getElementById("rdoAdd");
    var rdoMinus = document.getElementById("rdoMinus");
    var tblResult = document.getElementById("tblResult");
    var initialValue = 10000;
    var objAccount = new Compute(initialValue);

function printResult() {
    var display = "";
    var rdoAdd = document.getElementById("rdoAdd");
    var rdoMinus = document.getElementById("rdoMinus");
    var numOne = parseInt(document.getElementById('txtNumOne').value);

    if (rdoAdd.checked) {
    objAccount.addNum(numOne);    // This will directly adjust `initialValue`
    display = "<tr>";
    display += "<td>" + objAccount._initialValue + "</td>";
    display += "<tr>";
    tblResult.innerHTML += display;

    resetx();
    } else {
    objAccount.minusNum(numOne);    // This will directly adjust `initialValue`
    display = "<tr>";
    display += "<td>" + objAccount._initialValue + "</td>";
    display += "<tr>";
    tblResult.innerHTML += display;

    resetx();
    }
}
function resetx() {
    document.getElementById('txtNumOne').value = "";
    document.getElementById("rdoAdd").checked = false;
    document.getElementById("rdoMinus").checked = false;
}

</script>
</body>
</html>

答案 3 :(得分:0)

function Compute(initialNum) {
    this._initialNum = initialNum;

    this.addNum = function(numOne) {
        alert(this._initialNum = this._initialNum + numOne);
        return this._initialNum;
    };

    this.minusNum = function(numOne) {
        alert(this._initialNum = this._initialNum - numOne);
        return this._initialNum;
    };
}