JavaScript design mode 3

Source: Internet
Author: User

Portal Big Open Object

var book=function(isbn,title,author) {    ifthrownew Error ("illegal");     this. isbn=ISBN;     this. title=title| | ' No title ';     this. author| | ' No author ';} Book.prototype.display=function() {    ...};

Hardened version

varbook=function(isbn,title,author) {if(! This. CHECKISBN (ISBN))Throw NewError ("ISBN not valid");  This. isbn=ISBN;  This. title=title| | ' No title ';  This. author| | ' No author ';} Book.prototype={checkisbn:function(ISBN) {if(isbn==undefined| |typeofIsbn!= ' String ') {            return false; } ISBN=isbn.replace (/-/, "); if(isbn.length!=10 &&isbn.length!=13) {            return false; }        varSum=0; if(isbn.length===10) {            if(!isbn.match (/^\d{9}/)){                return false; }             for(vari=0;i<9;i++) {sum+=isbn.charat (i) * (10-i); }            varChecksum=sum%11; if(checksum===10) checksum= ' X '; if(Isbn.charat (9)! =checksum) {                return false; }        }        Else{            if(!isbn.match (/^\d{12}/)){                return false; }             for(vari=0;i<12;i++) {sum+=isbn.charat (i) * ((i%2===0)? 1:3); }            varChecksum=sum%10; if(Isbn.charat (12)! =checksum) {                return false; }        }        return true; }, display:function(){        ...    }};

Further

varpublication=NewInterface (' Publication ', [' getisbn ', ' setisbn ', ' getTitle ', ' settitle ', ' getauthor ', ' setauthor ', ' Display ']);varbook=function(isbn,title,author) { This. SETISBN (ISBN);  This. Settitle (title);  This. Setauthor (author);} Book.prototype={checkisbn:function(ISBN) {...}, GETISBN:function(){        return  This. ISBN; }, SETISBN:function(ISBN) {if(! This. CHECKISBN (ISBN))Throw NewError ("ISBN not valid");  This. isbn=ISBN; }, GetTitle:function(){        return  This. title; }, Settitle:function(title) { This. title=title| | " No title "; }, Getauthor:function(){        return  This. Author; }, Setauthor:function(author) { This. author=author| | " No author "; }, display:function(){        ...    }};

To prevent unintended use by other programmers, you can add _ to private methods and properties to indicate that they are private

Implementing private members with closures

varbook=function(newisbn,newtitle,newauthor) {varIsbn,title,author; functionCHECKISBN (ISBN) {...}  This. getisbn=function(){        returnISBN;    };  This. setisbn=function(NEWISBN) {if(!CHECKISBN (NEWISBN))Throw NewError ("ISBN not valid"); ISBN=NEWISBN;    };  This. gettitle=function(){        returntitle;    };  This. settitle=function(newtitle) {title=newtitle| | ' No title ';    };  This. getauthor=function(){        returnauthor;    };  This. setauthor=function(newauthor) {author=newauthor| | ' No author ';    };  This. SETISBN (NEWISBN);  This. Settitle (Newtitle);  This. Setauthor (Newauthor);}; Book.prototype={display:function(){        ...    }};

JavaScript design mode 3

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.