javascript - 如何在对象字面量中使用另一个 JS 函数的返回值

标签 javascript return-value

我有一系列的两个方法,其中第二个方法必须使用第一个方法的返回值。所以在我的代码中,如下所示,totalRoute需要从totalInvoiced返回的值(即value3)来计算其值,然后显示在我正在构建的寄存器中。 totalRoute 将等于 totalInvoiced 减去 notCollectednotCollectedLateexpenditure1 的总和> 和expenditure2

我的问题是,当我插入 notCollectednotCollectedLateexpenditure1expenditure2 的值时,该值totalRoute 的值为 NaN。我想这是因为我没有正确使用 totalInvoiced 返回的值,或者该值没有正确传递。为什么我的函数没有获取或使用 totalInvoiced 中的值?

My fiddle here

我的 JS 代码在这里:

var A = {
    today: document.getElementById("today"),

    displayDate: function () {
        var cD = new Date();
        var day = cD.getDate();
        var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec"];
        var year = cD.getFullYear();
        var today1 = A.today;

        today1.innerHTML = (day + "/" + months[cD.getMonth()] + "/" + year);
    },

    invoiced: document.getElementById("invoiced"),

    lastInvoiced: document.getElementById("lastinvoiced"),

    totalInvoiced: function () {

        var value1 = parseFloat(A.invoiced.value);

        var value2 = parseFloat(A.lastInvoiced.value);

        if (isNaN(value1))
            value1 = 0;
        if (isNaN(value2))
            value2 = 0;

        var totalInvoiced1 = value1 + value2;
        var value3 = document.getElementById("daytotal").value = totalInvoiced1 + "€";
        console.log(value3);
        return value3;
    },

    notCollected: document.getElementById("notcollected"),

    notCollectedLate: document.getElementById("notcollectedlate"),

    expenditure1: document.getElementById("expenditure1"),

    expenditure2: document.getElementById("expenditure2"),

    totalRoute: function () {
        var value4 = parseFloat(A.notCollected.value);
        var value5 = parseFloat(A.notCollectedLate.value);
        var value6 = parseFloat(A.expenditure1.value);
        var value7 = parseFloat(A.expenditure2.value);

        if (isNaN(value4))
            value4 = 0;
        if (isNaN(value5))
            value5 = 0;
        if (isNaN(value6))
            value6 = 0;
        if (isNaN(value7))
            value7 = 0;

        var totalExp = (value4 + value5 + value6 + value7);
        var value3 = A.totalInvoiced();
        var tRoute = value3 - totalExp;
        var value8 = document.getElementById("total").value = tRoute + "€";

        console.log(value8);
        return value8;
    }


};
window.onload = A.totalInvoiced();
window.onload = A.displayDate();

A.invoiced.addEventListener("change", A.totalInvoiced, false);
A.lastInvoiced.addEventListener("change", A.totalInvoiced, false);

A.notCollected.addEventListener("change", A.totalRoute, false);
A.notCollectedLate.addEventListener("change", A.totalRoute, false);
A.expenditure1.addEventListener("change", A.totalRoute, false);
A.expenditure2.addEventListener("change", A.totalRoute, false);

最佳答案

TotalInvoiced 中返回的

value3 包含欧元字符。只需将totalInvoiced 的返回值更改为totalInvoiced1 而不是value3,即可获得我认为您正在寻找的功能。

关于javascript - 如何在对象字面量中使用另一个 JS 函数的返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23113030/

相关文章:

javascript - 仅将单击的元素传递给 onClick 函数 - ReactJS

javascript - 删除 Laravel 表单之外的请求

programming-languages - 为什么大多数编程语言对 4 的平方根只给出一个答案?

c++ - 为什么返回值不同?

function - Common Lisp 函数没有返回值

jquery - 有什么方法可以让 window.onload 函数只执行一次吗?

javascript - 嵌套 react 路由器 : hide parent component on showing nested child component

javascript - 日期时间格式 正则表达式 YYYY/MM/DD hh :mm:ss

php - array_walk 还是 array_map?

c# - 将 ASP.Net 控件 ID 发送到 JavaScript 函数