zoukankan      html  css  js  c++  java
  • new Date()的数据类型的问题

    function getServerNow(){
        return new Date(new Date() + svrMinusLocal);
    }

    svrMinusLocal是服务器时间减本地时间的时间差,这代码有什么问题?

    console.log(new Date() + 1); // 这里的new Date()会返回字符串与"1"连接起来
    console.log(new Date() - 1); // 这里的new Date()会返回数字与数字1相减得到一个数字
    
    // 个人猜测 自动转换类型中, toString 比 valueOf 优先?于是做了如下实验
    
    var autoType = {
        toString: function(){
            return 'a';
        },
        valueOf: function(){
            return 1;
        }
    }
    console.log(autoType + 1);    // 2
    console.log(autoType + 's');    // 1s
    console.log(autoType - '1');    // 0
    // 证明上面的猜测是错的,呃
    // 那只能死记了
    点击查看答案以及个人猜想
  • 相关阅读:
    OC之class与metaclass
    call vs apply
    ABI
    WEB服务器的四种类型
    情绪控制
    位置无关代码
    Finding Leaks Using Instruments
    Mac之TwoLevel Namespace
    Apache的几个特性
    Mac之debuging symbol
  • 原文地址:https://www.cnblogs.com/arliang/p/auto_type_convertion_in_js.html
Copyright © 2011-2022 走看看