zoukankan      html  css  js  c++  java
  • Javascript设计模式之创建构造函数和方法

    构造函数和方法

    var Book = function (isbn,title,author) {
        this.setIsbn(isbn);
        this.setTitle(title);
        this.setAuthor(author);
    }
    
    Book.prototype = {
        checkIsbn:function (isbn) {
            // todo...
            return true;
        },
        getIsbn:function () {
            return this.isbn;
        },
        setIsbn:function (isbn) {
            if (!this.checkIsbn(isbn)) {
                throw new Error('Book:Invalid ISBN.');
            }
            this.isbn = isbn;
        },
        getTitle:function () {
            return this.title;
        },
        setTitle:function (title) {
            this.title = title || 'No title specified';
        },
        getAuthor:function () {
            return this.author;
        },
        setAuthor:function (author) {
            this.author = author || 'No author specified';
        },
        display:function() {
            // todo...
        }
    }
    
    var book = new Book('isbn','Javascript','Jim');
    console.log(book.getIsbn());
    console.log(book.getTitle());
    console.log(book.getAuthor());
    
    var book1 = new Book('isbn');
    console.log(book1.getIsbn());
    console.log(book1.getTitle());
    console.log(book1.getAuthor());
    
    
    

    isbn
    Javascript
    Jim
    isbn
    No title specified
    No author specified

    用命名规范区分私有变量

    var Book = function (isbn,title,author) {
        this.setIsbn(isbn);
        this.setTitle(title);
        this.setAuthor(author);
    }
    
    Book.prototype = {
        checkIsbn:function (isbn) {
            // todo...
            return true;
        },
        getIsbn:function () {
            return this._isbn;
        },
        setIsbn:function (isbn) {
            if (!this.checkIsbn(isbn)) {
                throw new Error('Book:Invalid ISBN.');
            }
            this._isbn = isbn;
        },
        getTitle:function () {
            return this._title;
        },
        setTitle:function (title) {
            this._title = title || 'No title specified';
        },
        getAuthor:function () {
            return this._author;
        },
        setAuthor:function (author) {
            this._author = author || 'No author specified';
        },
        display:function() {
            // todo...
        }
    }
    

    用闭包实现私有方法

    var Book = function (newIsbn,newTitle,newAuthor) {
        // 私有属性
        var isbn,title,author;
    
        // 私有方法
        function checkIsbn(isbn) {
            // todo
            return true;
        }
    
        // 保护方法
        this.getIsbn = function () {
            return isbn;
        };
    
        this.setIsbn = function (newIsbn) {
            if (!checkIsbn(newIsbn)) {
                throw new Error('Book:Invalid ISBN.');
            }
            isbn = newIsbn;
        };
    
        this.getTitle = function () {
            return title;
        };
    
        this.setTitle = function (newTitle) {
            title = newTitle || 'No title specified';
        };
    
        this.getAuthor = function () {
            return author;
        };
    
        this.setAuthor = function (newAuthor) {
            author = newAuthor || 'No author specified';
        };
    
        // 构造函数
        this.setIsbn(newIsbn);
        this.setTitle(newTitle);
        this.setAuthor(newAuthor);
    }
    
    // 公有方法
    Book.prototype = {
        display:function () {
            // todo...
        }
    }
    
    var book = new Book('isbn','Javascript','Jim');
    console.log(book.getIsbn());
    console.log(book.getTitle());
    console.log(book.getAuthor());
    

    静态方法和属性

    var Book = (function () {
        // 私有静态属性
        var numOfBooks = 0;
    
        // 私有静态方法
        function checkIsbn(isbn) {
            // todo...
            return true;
        }
    
        // 返回构造函数
        return function(newIsbn,newTitle,newAuthor) {
            // 私有属性
            var isbn,title,author;
    
            // 特权方法
            this.getIsbn = function () {
                return isbn;
            };
    
            this.setIsbn = function (newIsbn) {
                if (!checkIsbn(newIsbn)) {
                    throw new Error('Book:Invalid ISBN.');
                }
                isbn = newIsbn;
            };
    
            this.getTitle = function () {
                return title;
            };
    
            this.setTitle = function (newTitle) {
                title = newTitle || 'No title specified';
            };
    
            this.getAuthor = function () {
                return author;
            };
    
            this.setAuthor = function (newAuthor) {
                author = newAuthor || 'No author specified';
            };
    
            numOfBooks++;
            if (numOfBooks > 5) {
                throw new Error('Book:只允许创建5个Book对象');
            }
    
            this.setIsbn(newIsbn);
            this.setTitle(newTitle);
            this.setAuthor(newAuthor);
        }
    })();
    
    // 公有静态方法
    Book.convertToTitleCase = function (inputString) {
        // todo...
        return inputString;
    }
    
    // 公有方法
    Book.prototype = {
        display:function() {
            // todo...
        }
    }
    console.log(Book.convertToTitleCase('test')); // test
    var book = new Book('isbn','Javascript','Jim');
    console.log(book.getTitle());
    var book = new Book('isbn','Javascript','Jim');
    console.log(book.getTitle());
    var book = new Book('isbn','Javascript','Jim');
    console.log(book.getTitle());
    var book = new Book('isbn','Javascript','Jim');
    console.log(book.getTitle());
    var book = new Book('isbn','Javascript','Jim');
    console.log(book.getTitle());
    var book = new Book('isbn','Javascript','Jim');
    console.log(book.getTitle());
    

    test
    Javascript
    Javascript
    Javascript
    Javascript
    Javascript
    ...js:46 throw new Error('Book:只允许创建5个Book对象');

  • 相关阅读:
    页面后退的总结
    Flash Builder4.6 无法启动,并且报 Failed to create the Java Virtual Machine(1不行的话可以参考下2)
    单独的js代码文件被JSP文件调用,中文乱码问题
    Flash Builder4.6 破解方法的实践
    sql文学习.....关于条件判断的查询....casewhenthen
    解决flash builder 4.6安装过程中安装程序遇到错误(1)
    flex builder 4 控制台不能输出trace()的解决方法
    jstl遍历map,foreach
    jar包直接拷贝到WEBINF/lib下和以userLibrary形式引入的区别?/jar包放置在WEBINF/lib下和通过build path导入的区别是什么?
    flash build 4.6 不能debug 报错 C:\WINDOWS\system32\Macromed\Flash\NPSWF32.dll
  • 原文地址:https://www.cnblogs.com/jiqing9006/p/6206756.html
Copyright © 2011-2022 走看看