zoukankan      html  css  js  c++  java
  • JavaScript toString、String和stringify方法区别

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="utf-8" />
            <title>JavaScript toString与String方法区别</title>
        </head>
        <body>
            <script type="text/javascript">
                //一 toString限制
                // 报错Cannot read property 'toString' of undefined
                // console.log(undefined.toString())
                // 报错Cannot read property 'toString' of undefined
                //console.log(null.toString())
    
                //二 String无限制
                console.log(String(undefined))
                console.log(String(null))
    
                //三 toString可根据进制编码
                let num = 10;
                console.log(num.toString(2))
            </script>
        </body>
    </html>

     二、stringify也可以实现字符串化,并且健壮性也良好

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="utf-8" />
            <title>JavaScript toString、String和stringify方法区别</title>
        </head>
        <body>
            <script type="text/javascript">
                let a = {
                    age: undefined,
                    name: null
                }
                //输出 {"name":null}
                console.log(JSON.stringify(a))
                let b;
                //输出 undefined
                console.log(JSON.stringify(b));
                let c = null;
                //输出 null
                console.log(JSON.stringify(c))
            </script>
        </body>
    </html>
  • 相关阅读:
    vijos 1066 弱弱的战壕 树状数组
    vijos 1057 盖房子 简单DP
    完全背包
    HDU 1203 和 HDU 2191
    dp 01背包,完全背包,多重背包 模板
    UVA11624 Fire!
    我们要学习的算法
    Find a way 两路广搜
    NYOJ 最小步数(简单深搜与广搜)
    HDU Dungeon Master广搜
  • 原文地址:https://www.cnblogs.com/mengfangui/p/9909197.html
Copyright © 2011-2022 走看看