admin管理员组文章数量:1421943
[].valueOf()
method retuns array itself.According to this
document.write([["a"]],["b"])
should return ['a']b isn't it?But this is not happening ,it just writes ab
.I just wanted to know the reason behind this.
For string elements .toString() method returns this,
["a","b"].toString()//a,b
But for elements with array it should return
[["a"],"b"].toString()//[a],b
[].valueOf()
method retuns array itself.According to this
document.write([["a"]],["b"])
should return ['a']b isn't it?But this is not happening ,it just writes ab
.I just wanted to know the reason behind this.
For string elements .toString() method returns this,
["a","b"].toString()//a,b
But for elements with array it should return
[["a"],"b"].toString()//[a],b
Share
Improve this question
edited Jun 22, 2013 at 7:30
Maizere Pathak.Nepal
asked Jun 22, 2013 at 3:42
Maizere Pathak.NepalMaizere Pathak.Nepal
2,4114 gold badges30 silver badges41 bronze badges
2
-
I think it uses
toString()
notvalueOf()
– Arun P Johny Commented Jun 22, 2013 at 3:46 -
your edit's sample returns
a,b
, and no, the latter returnsa,b
because the[].toString()
casts all its items to string before concatenating - nested arrays will be stringified recursively. – Fabrício Matté Commented Jun 22, 2013 at 4:05
3 Answers
Reset to default 4When you pass an object to document.write, Javascript converts the object to a string with .toString(). In this case, Array.toString() will flatten and join the array with mas, and return it as a string.
["this", "is", "an", "array!"].toString(); // "this,is,an,array!"
[["a",["b"]], ["c"]].toString() // "a,b,c"
We can expand document.write([["a",["b"]], ["c"]])
into the following:
var input = [["a",["b"]], ["c"], "d"];
Array.prototype.verboseToString = function verboseToString() {
// Make a copy of the array, so we don't destroy the original
var copy = this.slice(), i;
for (i = 0; i < copy.length; i++) {
// If this is an Array, call verboseToString() on it, and go deeper
if (copy[i] instanceof Array === true) {
copy[i] = copy[i].verboseToString();
}
}
// copy contains non-arrays and we're ignoring other types' toString() output
return copy.join(',');
}
document.write(input.verboseToString()); // "a,b,c,d"
document.write([["a"]]",",["b"])
write get unlimited arguments separated by mas so it is actually expected behavior
In order to print what you want use:
document.write(["a","b"])
This way you will print an array and not a list of arrays
From docs
The text you write is parsed into the document's structure model.
So you send out an array it will just evaluate the array values to string to create a document structure which [["a"]],["b"]
has none but just the text values.
If you do this:
document.write(["<a>a</a>", "<a>b</a>"])
You can see it creates 2 anchor elements separated by ,
so its just array.join(',')
or just provide this:
document.write(["<a>a</a>"], ["<a>b</a>"])
This time it will create 2 anchors you don't see a ma anymore.
本文标签: Javascript array valueOf methodStack Overflow
版权声明:本文标题:Javascript array valueOf method - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745356004a2655055.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论