每天CookBook之JavaScript-043

  • 实现继承
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>043</title>
</head>
<body>
    
</body>
<script type="text/javascript">
(function () {
    function origObject(){
        this.val1 = 'a';
        this.val2 = 'b';
    }

    origObject.prototype.returnVal1 = function(){
        return this.val1;
    }

    origObject.prototype.returnVal2 = function(){
        return this.val2;
    }

    function newObject(){
        this.val3 = 'c';
        origObject.call(this);
    }

    newObject.prototype = Object.create(origObject.prototype);
    newObject.prototype.getValues = function(){
        return this.val1 + " " + this.val2 + " " + this.val3;
    }
    var obj = new newObject();
    console.log(obj instanceof(newObject));
    console.log(obj instanceof(origObject));
    console.log(obj.getValues());

})();

(function () {
    function Book (title, author){
        this.getTitle = function(){
            return "Title: " + title;
        };
        this.getAuthor = function(){
            return "Author: " + author;
        };
    }

    function TechBook (title, author, category) {
        this.getCategory = function(){
            return "Technical Category: " + category;
        }

        this.getBook = function(){
            return this.getTitle() + " " + author + " " + this.getCategory();
        }
        Book.apply(this, arguments);
    }

    TechBook.prototype = Object.create(Book.prototype);
    TechBook.prototype.constructor = TechBook;

    var newBook = new TechBook("The JavaScript Cookbook", "Shelley Powers", "Programming");
    console.log(newBook.getBook());

    console.log(newBook.getTitle());
    console.log(newBook.getAuthor());
    console.log(newBook.getCategory());
 })();  
</script>
</html>

转载于:https://www.cnblogs.com/4thing/p/5683080.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值