javascript - 一个js面试题
本文介绍了javascript - 一个js面试题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
function convert(s) {
console.log(s);//hi
s +=s.toUpperCase();
console.log(s);//hiHI
}
function funny(s) {
convert(s);
console.log(s);//hi
}
funny('hi');//求问为什么第三个是hi吗?为什么不是hiHI
解决方案
function convert(otherStr) {
console.log(otherStr);//hi
otherStr += otherStr.toUpperCase();
console.log(otherStr);//hiHI
}
function funny(str) {
convert(str);
console.log(str);//hi
}
funny('hi');
不知道这样是不是能看明白一点
另外如果想第三个想输出hiHI可以吧代码改成如下
function convert(otherStr) {
console.log(otherStr);//hi
otherStr += otherStr.toUpperCase();
console.log(otherStr);//hiHI
return otherStr
}
function funny(str) {
//用一个变量来接收convert返回的值,当然也可以不接收直接打印出来
var res = convert(str);
console.log(res);//hiHI
}
funny('hi');
这篇关于javascript - 一个js面试题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文