JavaScript的內置對象

HTML文檔:html

<html>
    <head>
        <meta charset="UTF-8">
        <title>HelloWorld</title>

        <script src="hello.js"></script>
    </head>

    <body>
    <form name="myForm">
        <input type="text" name="txtString">
        <input type="button" value="檢 查" onclick="check(document.myForm.txtString.value)">
        <hr>
        <input type="button" value="隨機數" onclick="getRandom()">
        <input type="button" value="計 算" onclick="calculator()">
    </form>
    </body>
</html>

Js文檔:數組

/*
    JavaScript對象:
        1.JavaScript的內置對象
        2.瀏覽器對象
        3.自定義對象

    字符串對象的建立:
        1.直接聲明字符串變量
            var myString="This is my String";
        2.使用new關鍵字來建立字符串對象
            var myString=new String("my String");

    字符串對象的經常使用屬性:
        Constructor :字符串對象的函數模型
        length      :字符串長度
        prototype   :添加字符串對象的屬性

    字符串對象的經常使用方法:
        charAt(位置)  :字符串在指定位置處的字符
        charCode(位置):字符串在指定位置處字符的Unicode值
        indexOf(要查找的字符串,[起始位置])
        lasIndexOf(要查找的字符串)
        subStr(開始位置,[長度])
        stbString(開始位置,結束位置)
        split([分割符號])
        replace(須要替換的字符串,新字符串)
        toLowerCase():
        toUpperCase():

 */
function check(subChar) {
    var findChar="abcdefghijklmnopqrstuvwxyz1234567890_-";

    for(var i=0;i<subChar.length;i++){
        if(findChar.indexOf(subChar.charAt(i))==-1){
            alert("你輸入的字符串不合法");
            return;
        }
    }
    alert("你輸入的字符串合法")
}
/*
    數學對象的屬性:
        E      :歐拉常熟,天然對數的底
        LN2    :2的天然對數
        LN10   :10的天然對數
        LOG2E  :2爲底的e的天然天然對數
        LOG10E :10爲底的e的天然對數
        PI     :圓周率
        SQRT1_2:0.5的平方根
        SQRT2  :2的平方根

    數學對象的方法:
        abs()、acos()、asin()、atan()、ce il()、cos()
        floor()、log()、max()、min()、pow()、random()
        round()、sin()、sqrt()、tan()
 */

var data;

function getRandom() {
    data = Math.floor(Math.random() * 101);
    alert("隨機整數爲:" + data);
    return data;
}

function calculator(){
    var square = Math.pow(data, 2);
    var squareRoot = Math.sqrt(data).toFixed(2);
    var logarithm = Math.log(data).toFixed(2);
    alert("隨機整數"+data+"相關計算:\n\t平方\t平方根\t天然對數\n\t"+square+"\t"+squareRoot+"\t"+logarithm)
}
/*
        3.日期對象
 */
/*
        數組對象的建立:
            a.新建一個長度爲零的數組
                var myArr = new Array();
            b.新建一個長度爲n的數組
                var myArr = new Array(n);
            c.新間一個數組,並初始化
                var myArray = new Array(1,2,3,4)

 */
相關文章
相關標籤/搜索