zoukankan      html  css  js  c++  java
  • javascript中toString跟toLocaleString的区别

    toString:无参数,返回一个表示调用这个方法的对象值的字符串。在需要将对象转换为字符串的时候,javaScript都会调用这个方法。

    默认的toString()方法的返回值带有的信息量很少,(不过在检测对象的类型时非常有用);

    var s = {x:1,y:1}.toString(); 代码的计算结果为字符串"[Object Object]" 

    toLocaleString():对象都存在toLocaleString()方法,这个方法返回一个表示这个对象的本地化字符;

    在Object中默认的toLocaleString()方法并不做任何本地化自身的操作,它仅调用toString()方法并返回对应值。Date和Number类对toLocaleString()方法做了定制,可以用它对数字、日期和时间做本地化的转换。Array类的toLocaleString()方法和toString方法很像,唯一的不同是每个数组元素会调用toLocaleString()方法转换为字符串,而不是调用各自的toString()方法;

    Number对象:

    var e=123
    e.toString()
    "123"
    e.toLocaleString()
    "123"
    Array对象:
    var aa=[1,2,3]
    aa.toLocaleString()
    "1,2,3"
    aa.toString()
    "1,2,3"
    Date对象:
    var sd=new Date()
    sd
    Wed Feb 15 2017 11:21:31 GMT+0800 (CST)
    sd.toLocaleString()
    "2017/2/15 上午11:21:31"
    sd.toString()
    "Wed Feb 15 2017 11:21:31 GMT+0800 (CST)"


  • 相关阅读:
    python读取csv数据(添加列名,指定分隔方式)
    loc_survived
    数据预处理
    hadoop 指令
    pd.concat
    DataFrame
    SQL左连接
    mysql mysql之把查询的结果保存到新表(小知识点)
    啦啦啦啦 mysql 授权
    ArrayList和LinkedList的区别以及优缺点
  • 原文地址:https://www.cnblogs.com/zhirusi/p/7824931.html
Copyright © 2011-2022 走看看