面向對象編程

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>面向對象編程</title>
    <meta charset="utf-8" />
</head>
<body>    
    <script>
        var person = {
            name: "張三",
            age: 18,
            getName: function () {
                return this.name;
            }
        }
    </script>
    <script>
        function parent() {
            this.name = "張三";
            this.show1 = function () {
                alert("我是" + this.name);
            }
        }
        function child() {
            this.age = 18;
            this.show2 = function () {
                alert("個人年齡是"+this.age)
            }
        }
        child.prototype = new parent();//child繼承parent
        var test = new child();
        test.name = "李四";//繼承父對象的name屬性,並修改屬性值
        test.show1();//繼承父對象的show1()方法
        test.show2();//調用子對象的show2()方法
    </script>

</body>
</html>
相關文章
相關標籤/搜索