JavaScript內(nèi)置對象

String對象

<title>String對象</title>
<script type="text/javascript">
    //方式一:定義String對象
    /*
    var str1 = new String("hello");
    var str2 = new String("hello");
    
    document.write("結(jié)果:"+(str1==str2)+"<br/>");  false
    //valueOf():該方法返回對象的內(nèi)容
    document.write("結(jié)果:"+(str1.valueOf()==str2.valueOf()));   true
    */
    
    //方式二:
    /*
    var str1 = "hello";
    var str2 = "hello";
    
    document.write("結(jié)果:"+(str1==str2)+"<br/>");  true
    */
    
    /*
    常用方法:
        charAt(): 返指定索引的內(nèi)容回
        indexOf(): 返回首次出現(xiàn)指定字符的索引位置
        lastIndexOf(): 返回最后出現(xiàn)指定字符的索引位置
        fontcolor(): 直接給字符串添加顏色
        replace(): 替換指定的字符串
        split(): 使用指定字符串切割字符串,返回字符串?dāng)?shù)組
        substring(start,end); 截取字符串,start:開始索引, end:結(jié)束索引
        substr(strat,[length]) 截取字符串, start:開始索引 length: 截取的字符串長度
    */
    
    var str ="hellojava";
    document.write(str.charAt(4)+"<br/>");
    document.write(str.indexOf("a")+"<br/>");
    document.write(str.lastIndexOf("a")+"<br/>");
    document.write(str.fontcolor("#0000ff")+"<br/>");
    document.write(str.replace("java","itcast")+"<br/>");
    document.write(str.substring(5,9)+"<br/>");
    document.write(str.substr(5,4));
    
    var str = "java-net-php-平面";
    var strArray = str.split("-");
    for(var i=0;i<strArray.length;i++){
        document.write(strArray[i]+",");    
    }

</script>

Number對象

<title>Number對象</title>
<script type="text/javascript">
    //方式一:定義Number對象
    /*
    var num1 = new Number(20);
    var num2 = new Number(20);
    */
    
    //方式二:
    /*
    var num1 = 20;
    var num2 = 20;
    
    document.write((num1==num2)+"<br/>");
    document.write(num1.valueOf()==num2.valueOf());
    */
    
    var b1 = new Boolean(true);
    var b2 = new Boolean(true);
    document.write((b1==b2)+"<br/>");
    document.write(b1.valueOf()==b2.valueOf());
    
</script>

Math對象

<script type="text/javascript">
    /*
    常用的方法:
        1)ceil(): 向上取整。 如果有小數(shù)部分的話,直接+1
        2)floor(): 向下取整。如果有小數(shù)部分的話,直接丟失小數(shù)部分,保利整數(shù)位
        3) round(): 四舍五入取整。滿5進(jìn)一
        4)random(): 生成一個隨機(jī)的0-1的小數(shù) .包含0,不包含1
        5)max(): 返回最大值
        6)min(): 返回最小值

    */
    var num = 3.50;
    document.write(Math.ceil(num)+"<br/>");
    document.write(Math.floor(num)+"<br/>");
    document.write(Math.round(num)+"<br/>");
    document.write(Math.round(Math.random()*100)+"<br/>");
    document.write(Math.max(10,6,54,23,76)+"<br/>");
    document.write(Math.min(10,6,54,23,76)+"<br/>");

</script>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容