【JavaScript】JavaScript高級教程

參考鏈接:
1、https://www.w3cschool.cn/javascript/

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
</head>
<body>

    <p id="demo">This is paragraph.</p>
    <button onclick="upArray()">upArray</button>
    <button onclick="LowerArray()">LowerArray</button>
    <br>
    <a href="javascript:window.opener=null;window.open('','_self');window.close();">close window</a>
    <br>
    <span id="displayTime">This window open time (s): 0</span>
    <button onclick="myChangeFunction()" id="ChangeBtn">Stop</button>
    <br>

    <!--  next is javascript -->
    <script text="text/javascript">

        function person(firstname, lastname, age, eyecolor) {
            this.firstname = firstname;
            this.lastname = lastname;
            this.age = age;
            this.eyecolor = eyecolor;
            this.changeName = changeName;
            function changeName(name) {
                this.lastname = name;
            }
        }
        myMother = new person("Sally", "Rally", 48, "green");
        myMother.changeName("Doe");
        document.write(myMother.lastname + "<br>");
        document.write("Object JSON:" + JSON.stringify(myMother) + "<br>");
        // for object
        for(tempObj in myMother)
        {
            document.write(tempObj + "->" + myMother[tempObj] + "<br>");
        }
        // Number object
        Number.prototype.myadd = function()
        {
            var thisp = this;
            var y = new Number(1);
            var add1 = thisp + y;
            //this = this + 1;
            var type1 = typeof (thisp);
            var type2 = typeof (add1);
            var type3 = typeof (Number(add1));

        }
        var myNumber = 128;
        myNumber.toString(16);   // returns 80
        myNumber.toString(8);    // returns 200
        myNumber.toString(2);    // returns 10000000
        myNumber.myadd();
        // String object
        var myString = "12345";
        document.write("String to number: " + Number(myString) + 1);// 123451
        document.write("<br>");
        document.write("String to number: " + (Number(myString) + 1));// 12346
        document.write("<br>");
        // Array add new function to Array
        Array.prototype.myUcase = function () {
            for (i = 0; i < this.length; i++) {
                this[i] = this[i].toUpperCase();
            }
        }
        Array.prototype.lowerAll = function()
        {
            for(i = 0;i < this.length;i++)
            {
                this[i] = this[i].toLowerCase() + "_x";
            }
        }
        function upArray() {
            var fruits = ["Banana", "Orange", "Apple", "Mango"];
            fruits.myUcase();
            var x = document.getElementById("demo");
            x.innerHTML = fruits;
        }
        function LowerArray() {
            var fruits = ["Banana", "Orange", "Apple", "Mango"];
            fruits.lowerAll();
            var x = document.getElementById("demo");
            x.innerHTML = fruits;
        }
        // windows object
        document.write("windows object: " + window.navigator.appName + "<br>");
        document.write("windows object: " + window.navigator.appVersion + "<br>");
        document.write("windows object: " + window.navigator.appCodeName + "<br>");
        window.resizeTo(500, 500);
        //window.close();
        // pop-up windows
        var yourName = window.prompt("Please input your name:");
        if (yourName != null && yourName != "") {
            document.write("Your name is " + yourName + ".<br>");
        }
        else {
            document.write("you not input your name  please try again!");
        }
        alert("Hello\nHow are you?");// \n is LF new line character in alert
        // setTimeout and setInterval
        var intervalHandle = window.setInterval(function () { myIsThread() }, 1000);
        var conterThreadRunTime = 0;
        function myIsThread() {
            conterThreadRunTime++;
            document.getElementById("displayTime").innerHTML = "This window open time (s): " + conterThreadRunTime;
        }
        function myChangeFunction() {
            var btnString = document.getElementById("ChangeBtn").innerHTML;
            if (btnString == "Stop") {
                window.clearInterval(intervalHandle);
                document.getElementById("ChangeBtn").innerHTML = "Start";
            } else if (btnString == "Start") {
                intervalHandle = window.setInterval(function () { myIsThread() }, 1000);
                document.getElementById("ChangeBtn").innerHTML = "Stop";
            }
        }
        // use Cookie
        document.cookie = "username=xiaogongwei; path=/xiaogongwei123";

    </script>
</body>
</html>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章