使用一个函数作为另一个函数的参数 [英] Using a function as a parameter to another function

查看:143
本文介绍了使用一个函数作为另一个函数的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

"use strict";
function square(x) {
    return x * x;
}
function add(square,y) {
    return square(4) +y
    document.write(square + y)
}
add(4,1);
console.log(add(4,1));

我必须在另一个函数的参数中使用一个函数.我正在尝试输出17.

I have to use a function as in the parameter of another function. I am trying to output 17.

推荐答案

只需这样称呼即可:

add(square,1);

您可能还想更改

function add(square,y) {
    return square(4) +y
    document.write(square + y)
}

成为:

function add(square,y) {
    document.write(square(4) + y)        
    return square(4) +y
}

根据安德鲁(Andrew)的评论,将document.write调用更改为console.log并打开浏览器控制台以读取结果也是一个不错的选择.

Per Andrew's comment, it might also be a good choice to change document.write calls to be console.log and open the browser console to read results.

这篇关于使用一个函数作为另一个函数的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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