JavaScript:将匿名函数分配给变量时,不传递函数返回值,而是将函数作为字符串传递 [英] JavaScript: When assigning an anonymous function to a variable, function return value is not passed, rather the function as a string

查看:21
本文介绍了JavaScript:将匿名函数分配给变量时,不传递函数返回值,而是将函数作为字符串传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习 JavaScript,但遇到了一个障碍.如果答案很明显并且可以通过简单的搜索获得,我提前道歉.我是编程和 JavaScript 的新手,不确定要遵循哪条查询线.

I am trying to learn JavaScript but I've come across a hurdle. If the answer is obvious and reachable through a simple search I apologize in advance. I am a novice to programming and JavaScript, and unsure what line of inquiry to follow.

在以下代码中,该函数从 HTML 表单中获取值,进行一些处理并将它们发回.我已经测试了输入和输出过程,它工作正常.

In the following code, the function takes values from a HTML form, does some processing and sends them back. I've tested the input and output process and it's working correctly.

function foo() {

var x = parseInt(document.formdata.fieldone.value);
var y = parseFloat(document.formdata.fieldtwo.value);

if (isNaN(y))
    { var z = x; }
else
    { var z = function(x, y) {
            if ((y * (x / 100)) < 1) {
                return (x + Math.ceil(y * (x / 100))); }
            else if ((y * (x / 100)) > 1) {
                return (x + Math.round(y * (x / 100))); }
            else {
                return 0; } } }

var bar = document.getElementById("output");

bar.innerHTML = z; }

问题是,当条件语句的else分支试图处理匿名函数时,返回值没有赋值;而是将整个函数作为一个字符串.即在 HTML 页面中出现以下内容:

The problem is, when the else branch of the conditional statement tries to process the anonymous function, the return value isn't assigned; rather the entirety of the function as a string. That is, the following appears in the HTML page:

function (x, y) { if ((y * (x/100)) <1) { return (x + Math.ceil(y * (x/100)));} else if ((y * (x/100)) > 1) { return (x + Math.round(y * (x/100)));}其他{返回0;} }

function (x, y) { if ((y * (x / 100)) < 1) { return (x + Math.ceil(y * (x / 100))); } else if ((y * (x / 100)) > 1) { return (x + Math.round(y * (x / 100))); } else { return 0; } }

我已经在 Chrome 和 Firefox 中测试了代码,结果是一样的.

I've tested the code in Chrome and Firefox and the result is the same.

感谢任何帮助,并提前感谢您.

Any help is appreciated and thank you in advance.

推荐答案

您需要通过传递两个参数来调用该函数,否则 z 变量将只存储对此函数的引用,但它不会评估它:

You need to call the function by passing it two arguments, because otherwise the z variable will just store a reference to this function but it will not evaluate it:

var z = (function(x, y) {
    if ((y * (x / 100)) < 1) {
        return (x + Math.ceil(y * (x / 100))); }
    else if ((y * (x / 100)) > 1) {
        return (x + Math.round(y * (x / 100))); }
    else {
        return 0; 
    } 
})(x, y);

请注意,匿名函数内部使用的(x, y)与最后作为参数传递的不同,对应于开头声明的两个变量foo 函数.

Note that (x, y) used inside the anonymous function are not the same as the one passed as arguments at the end which correspond to the two variables declared in the beginning of the foo function.

这篇关于JavaScript:将匿名函数分配给变量时,不传递函数返回值,而是将函数作为字符串传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆