function a(fn){ let str = '123' fn(str) console.log(str) //这里要打印出 456,不能修改a函数代码 } a)
时间: 2023-04-01 17:00:53 浏览: 56
可以通过在fn函数中修改str的值来实现打印出456,例如:
function fn(str){
str = '456'
}
function a(fn){
let str = '123'
fn(str)
console.log(str)
}
a(fn) //输出456
相关问题
在不改变a函数的同时 修改str为456function a(fn){ let str = '123' fn(str) } a(function(str){ str = '456' })
可以使用传递参数的方式来修改str,如下所示:
function a(fn){
let str = '123'
fn(str)
console.log(str) // 输出456
}
a(function(str){
str = '456'
return str
})
在不修改a函数的同时 改变a函数里str的值function a(fn){ let str = '123' fn(str) } a(function(str){ str = '456' })
可以使用传递对象的方式来改变a函数里str的值,代码如下:
function a(fn){
let obj = {str: '123'}
fn(obj)
console.log(obj.str)
}
a(function(obj){
obj.str = '456'
})
输出结果为:456
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)