js中的转换方法

1、var colors =['red','blue','green'];

     console.log(colors.toString()); // red,blue,green  

   总结:toString()方法 会返回数组中每个字符串形式拼接而成的以一个逗号分开的字符串

2、var colors =['red','blue','green'];

  console.log(colors.toString()); // red,blue,green  

     console.log(colors.valueOf()); // ['red','blue','green']

   总结:.valueOf()方法 会返回的是数组本身 

3、var colors =['red','blue','green'];

  alert(colors.toString()); // red,blue,green  

    alert(colors.valueOf()); // red,blue,green

alert(colors); // red,blue,green  

  延伸:就是用toString()方法可以用console.log实现alert数组的效果

     

猜你喜欢

转载自www.cnblogs.com/xyn0909/p/8963350.html