美文网首页
JavaScript-对象

JavaScript-对象

作者: 通灵路耳 | 来源:发表于2020-06-02 09:54 被阅读0次
对象就是属性+方法,有点Java的样子,JavaScript有自己的对象,下面我们看看吧

Number

    <body>
        <script>
            var x = new Number(123)
            document.write("类型:"+typeof(x))
            document.write("</br>")
            var y = 10
            document.write("类型:"+typeof(y))
            document.write("</br>")
            document.write("二进制:"+y.toString(2))
            document.write("</br>")
            document.write("二进制:"+y.toString(8))
            document.write("</br>")
            document.write("二进制:"+y.toString(16))
        </script>
    </body>

字符串

    <body>
        <script>
            var x = new String("张三")
            document.write("字符串长度:"+x.length)
            document.write("</br>")
            document.write("返回指定字符串:"+x.charAt(0))
            document.write("</br>")
            var y = new String("李四")
            document.write("连接:"+x.concat(y))
            document.write("</br>")
            document.write("字符串第一次出现位置:"+x.indexOf("三"))
            document.write("</br>")
            document.write("字符串最后一次出现位置:"+x.lastIndexOf("三"))
            document.write("</br>")
            document.write("判断字符串是否相等(1表示不相等):"+x.localeCompare(y))
            document.write("</br>")
            document.write("截取字符串:"+x.substring(0,1))
        </script>
    </body>

数组

    <body>
        <script>
            var x = new Array(5);
            var y = new Array(2,4,6,7,9);
            for(var i=0;i<y.length; i++){
                document.write(y[i])
            }
            
            document.write("</br> 长度:"+y.length)
        </script>
    </body>

Date日期

    <body>
        <script>
            var a = new Date();
            document.write(a.getFullYear()+"年"+(a.getMonth()+1)+"月"+a.getDate()+"日")
            document.write(a.getHours()+"时"+a.getMinutes()+"分"+a.getSeconds()+"秒")
        </script>
    </body>

Math

    <body>
        <script>
            document.write("圆周率:"+Math.PI)
            document.write("</br>")
            document.write("绝对值:"+Math.abs(-1))
            document.write("</br>")
            document.write("最小:"+Math.min(1,100)+"最大:"+Math.max(1,100))
            document.write("</br>")
            document.write("求幂:"+Math.pow(3,3))
            document.write("</br>")
            document.write("求幂:"+Math.round(3.4))
        </script>
    </body>

相关文章

网友评论

      本文标题:JavaScript-对象

      本文链接:https://www.haomeiwen.com/subject/euyyzhtx.html