Array, String and .join( )
This question already has an answer here:
It almost works as you wrote it. If you return str from the function, you can set newstr that way.
//empty string var newStr ='';
var myArr = ['it','was', 'the', 'best', 'of', 'times'];
var rejoiner = function (arr) {
var str = arr.join(' '); //checking that the function is working
console.log(str);
return str;
};
newStr = rejoiner(myArr);
Function parameters are copies. It's as if you'd written
var arr = myArr, str = newStr;
str = arr.join(' ');
console.log(str);
Assigning to str
doesn't change newStr
. In JavaScript, f(x)
can't change x
.
It just worked fine for me!:
//empty string
var newStr ='';
var myArr = ['it','was', 'the', 'best', 'of', 'times'];
var rejoiner = function (arr, str) {
str = arr.join(' ');
//checking that the function is working
console.log(str);
}
rejoiner(myArr, newStr);
Check your console log!
链接地址: http://www.djcxy.com/p/20996.html上一篇: 为什么在传递对象时使用'ref'关键字?
下一篇: 数组,字符串和.join()