每天CookBook之JavaScript-049

  • 链式方法调用
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>049</title>
</head>
<body>
    
</body>
<script type="text/javascript">
(function () {
    function Book (title, author){
        this.getTitle = function(){
            return "Title: " + title;
        };

        this.getAuthor = function(){
            return "Author: " + author;
        };

        this.replaceTitle = function(newTitle){
            var ordTitle = title;
            title = newTitle;
        };

        this.replaceAuthor = function(newAuthor){
            var oldAuthor = author;
            author = newAuthor;
        };
    }

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

        Book.apply(this, arguments);

        this.changeAuthor = function(newAuthor){
            this.replaceAuthor(newAuthor);
            return this;
        };
    }

    var newBook = new TechBook("I Know Things", "Smart Author", "tech");
    console.log(newBook.changeAuthor("Book k. Reader").getAuthor());
})(); 
</script>
</html>
posted @ 2016-07-20 22:59  4Thing  阅读(125)  评论(0编辑  收藏  举报