每天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>
posted @ 2016-07-18 23:13  4Thing  阅读(93)  评论(0编辑  收藏  举报