javascript - js新手关于参数传递的一个问题
大家讲道理
大家讲道理 2017-04-11 10:18:04
[JavaScript讨论组]

关于参数传递的一个问题,希望能够得到讲解和答疑

var timesTwo = function(number) {
    return number * 2;
};
console.log(timesTwo(8));

var newNumber = function(timesTwo){
    console.log(newNumber);
}
newNumber(5);

代码的结果输出
16
[Function]

我想知道,在函数 newNumber中 为什么没有传入timesTwo 函数返回的参数?

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

全部回复(5)
伊谢尔伦
var newNumber = function(n){
    var result=timesTwo(n)
    console.log(result);
}

你说的哪个练习题的答案就是让你调用一下timesTwo


// Parameter is a number, and we do math with that parameter
var timesTwo = function(number) {
    return number * 2;
};

// Call timesTwo here!
var newNumber = timesTwo(5);
console.log(newNumber);
伊谢尔伦

在函数 newNumber 中, timesTwo 是形参,你传入了5。当然了,不管你传什么都会 log 出你的函数体。

怪我咯

var timesTwo = function(number) {
return number * 2;
};

console.log(timesTwo(8));

var newNumber = function(timesTwo){
console.log(newNumber);
}

newNumber(timesTwo(8));

大家讲道理

形参根本没调用,所以不会有返回值这种情况

伊谢尔伦

var newNumber = function(num){
var result = timesTwo.call(null,num);
console.log(result);
}
newNumber(5);

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号