js 構造函數繼承

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
	<meta charset="utf-8" />
</head>
<body>

</body>
</html>

<script>

    function base() {
        this.name = 'zhang';
        this.age = 26;

        this.getName = function () {
            return this.name;
        };

        this.setName = function (newName) {
            this.name = newName;
        };
    }

    function son() {
        base.call(this);
    }

    var sonObject = new son();
    

    // 只會繼承屬性,各自擁有本身的屬性.
    var sonObject2 = new son();

    sonObject.setName('chao');
    console.log(sonObject);

    sonObject2.setName('hello');
    console.log(sonObject2);

</script>
相關文章
相關標籤/搜索