时间:2023-01-07 07:53:01来源:本站整理作者:点击:
一、toLocaleString()、toString()和valueOf()方法
在ECMAScript中,所有对象都具有toLocaleString()、toString()和valueOf()方法,数组也从Object类型继承这三个方法。
var colors = ["red", "blue", "green"]; alert(colors.toString()); //red,blue,green alert(colors.toLocalString()); //red,blue,green alert(colors.valueOf()); //red,blue,green alert(colors); //red,blue,green
由于alert()要接受字符串参数,所以它会在后台调用toString()方法,由此会得到与直接调用toString()方法相同的结果。
var person1 = { toLocaleString: function(){ return "Nikolaos"; }, toString: function(){ return "Nicholas"; } }; var person2 = { toLocaleString: function(){ return "Grigorios"; }, toString: function(){ return "Greg"; } }; var people = [people1, people2]; alert(people); //Nicholas,Greg alert(people.toString()); //Nicholas,Greg alert(people.toLocaleString()); //Nikolaos,Grigorios
二、join()方法
使用数组的toLocaleString()、toString()方法,在默认情况下会以逗号分隔的字符串的形式返回数组项。而如果使用join()方法,则可以使用不同的分隔符来构建这个字符串。join()方法只接收一个参数,即用作分隔符的字符串,然后返回包含所有数组项的字符串。如果不给join()方法传入任何值,或者给它传入undefined,则使用逗号作为分隔符。
var colors = ["red", "blue", "green"]; alert(colors.join("||")); //red||blue||green
如果数组中的某一项的值是null或者undefined,那么该值在join()、toLocaleString()、toString()方法返回的结果中以空字符串表示。
,“关公”陆树铭因病去世,网爆大衣哥亲往西安悼念,被赞有情有义“关公”陆树铭因病去世,网爆大衣哥亲往西安悼念,被赞有情有义
《卿卿日常》小说大结局是什么 尹峥和李薇最后在一起了吗《卿卿日常》24节气姑娘结局是什么_《卿卿日常》24节气姑娘离开老三了吗《点燃我温暖你》高见鸿背叛李峋了吗 高见鸿结局是什么《回来的女儿》原著小说介绍_《回来的女儿》改编自哪一部原著小说Copyright 2022-2026 feiyundao.com 〖妃孕岛〗 版权所有 备案号 :陕ICP备2022000637号-3
声明: 本站文章均来自互联网,不代表本站观点 如有异议 请与本站联系 本站为非赢利性网站 不接受任何赞助和广告