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

查看:96
本文介绍了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))); } else {return 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天全站免登陆