代码应该可以工作。如果控制台“显示逗号分隔的字符串”,这应该是字符串或数组。
如果adRotate(["one,link", "second,link,"])
- 链接是Array
- 你会得到:
function adRotate(links) {
var id = Math.floor(Math.random()*links.length); // valid index in links
var str = links[id]; // selects one of the links: str is a String
var item = str.split(','); // splits the string to an Array
console.debug(item); // logs the array
}
可能的结果:["one","link"]
或["second","link"]
如果adRotate("one link, and second")
- 链接是String
- 你会得到:
function adRotate(links) {
var id = Math.floor(Math.random()*links.length); // valid index in links
var str = links[id]; // selects one of the chars in the string: str is a String of length 1
var item = str.split(','); // splits the one-char-string to an Array
console.debug(item); // logs the array
}
可能的结果:["o"]
, ["n"]
, ["e"]
, [" "]
, ..., ["k"]
, ["",""]
(用于逗号字符), ["i"]
等。