且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Javascript设计模式之创建构造函数和方法

更新时间:2022-08-25 19:22:43

构造函数和方法

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对象');



本文转自TBHacker博客园博客,原文链接:http://www.cnblogs.com/jiqing9006/p/6206756.html,如需转载请自行联系原作者