var Anim = function () {} Anim.method('start', function () {}); Anim.method('stop', function () {});
//面向对象编程4 Function.prototype.method = function (name, fn) { this.prototype[name] = fn; returnthis; } var Anim = function () {} Anim.method('start', function () {}).method('stop', function () {});
//门户开放型 //无法检查isbn数据完整性--添加验证 var Book = function (isbn, title, author) { if (isbn = undefined) thrownewError('Book Constructor requires an isbn'); this.isbn = isbn; this.title = title || 'No title specified'; this.author = author || 'No author specified'; } Book.prototype.display = function () {};
//添加验证 //可以直接修改isbn的值,使得验证无效,因为该验证只是在构造器中--使用get/set var Book = function (isbn, title, author) { if (!this.checkIsbn(isbn)) thrownewError('Book: Invaild IsBN'); this.isbn = isbn; this.title = title; this.author = author; }
Book.prototype = { checkIsbn: function (isbn) { if (isbn == undefined || typeof isbn != 'string') { returnfalse; } isbn = isbn.replace(/-/, ''); if (isbn.length != 10 && isbn.length != 13) { returnfalse; }
var sum = 0; if (isbn.length === 10) { if (!isbn.match(/^\d{9}/)) { returnfalse; }
for (var i = 0; i < 9; i++) { sum += isbn.charAt(i) * (10 - i); }
var checksum = sum % 11; if (checksum === 10) checksum = 'X'; if (isbn.charAt(9) != checksum) { returnfalse; } } else { if (!isbn.match(/^\d{12}/)) { returnfalse; } for (var i = 0; i < 12; i++) { sum += isbn.charAt(i) * ((i % 2 === 0) ? 1 : 3); }
var checksum = sum % 10; if (isbn.charAt(12) != checksum) { returnfalse; } } returntrue; }, display: function () {} }; //GETTER/SETTE //使用赋值器,每次赋值都会验证,但仍然可以直接设置,因为属性是公共属性--特权方法 var Book = function (isbn, title, author) { this.setIsbn(isbn); this.setTitle(title); this.setAuthor(author); }
Book.prototype = { checkIsbn: function (isbn) {}, getIsbn: function () { returnthis.isbn; }, setIsbn: function (isbn) { if (!this.checkIsbn(isbn)) thrownewError('Book :Invalid isbn'); this.isbn = isbn; }, getTitle: function () { returnthis.title; }, setTitle: function (title) { this.title = title || 'No title specified'; }, getAuthor: function () { returnthis.author; }, setAuthor: function (author) { this.author = author || 'No author specified'; }, display: function () {} } //特权方法:利用函数作用域 //特权方法太多,会占用内存,因为每个对象,都有特权方法的副本--静态方法和属性 var Book = function (newIsbn, newTitle, newAuthor) { var isbn, title, author; //私有属性 functioncheckIsbn(isbn) {}; //私有方法 //特权方法 this.getIsbn = function () { return isbn; }; this.setIsbn = function (newIsbn) { if (!checkIsbn(newIsbn)) thrownewError('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 () {} };
//闭包:立即执行函数,正是实现私有化 //实例化是调用内层构造函数 var Book = (function () { var numOfBooks = 0;//静态属性 functioncheckIsbn(isbn) {}//静态方法
returnfunction (newIsbn, newTitle, newAuthor) { var isbn, title, author; this.getIsbn = function () { return isbn; }; this.setIsbn = function (isbn) { if (!checkIsbn(isbn)) thrownewError('Book :Invalid isbn'); isbn = isbn; }; this.getTitle = function () { return title; }; this.setTitle = function (title) { title = title || 'No title specified'; }; this.getAuthor = function () { return author; }; this.setAuthor = function (author) { author = author || 'No author specified'; };
MyNamespace.Singleton = (function () { var uniqueInstance; //私有属性,存储单体实例化对象 functionconstructor() { //private members var privateAttribute1 = false; var privateAttribute2 = 10;
functionprivateMethod1() {}
functionprivateMethod2() {} return { //public members publicAttribute1: true, publicAttribute2: 10, publicMethod1: function () {}, publicMethod2: function () {} } }
return { getInstance: function () { if (!uniqueInstance) { uniqueInstance = constructor(); } return uniqueInstance; } } })();