JS设计模式(二)getter setter

使用隐藏类信息的方法实现模拟getset

 

这是完全暴露类信息的代码

  1. var Book = function(isbn, title, author) {
  2.         if(isbn == undefined) throw new Error('Book constructor requires an isbn.');
  3.         this.isbn = isbn;
  4.         this.title = title || 'No title specified';
  5.         this.author = author || 'No author specified';
  6. }
  7. Book.prototype.display = function() {
  8. ...
  9. };

这是使用 Private Methods Using a Naming Convention 私有方法将信息隐藏

 

  1. var Book = function(isbn, title, author) { // implements Publication
  2. this.setIsbn(isbn);
  3. this.setTitle(title);
  4. this.setAuthor(author);
  5. }
  6. Book.prototype = {
  7. checkIsbn: function(isbn) {
  8. ...
  9. },
  10. getIsbn: function() {
  11. return this._isbn;
  12. },
  13. setIsbn: function(isbn) {
  14. if(!this.checkIsbn(isbn)) throw new Error('Book: Invalid ISBN.');
  15. this._isbn = isbn;
  16. },
  17. getTitle: function() {
  18. return this._title;
  19. },
  20. setTitle: function(title) {
  21. this._title = title || 'No title specified';
  22. },
  23. getAuthor: function() {
  24. return this._author;
  25. },
  26. setAuthor: function(author) {
  27. this._author = author || 'No author specified';
  28. },
  29. display: function() {
  30. ...
  31. }
  32. };

第三种方法 Scope,Nested Functions, and Closures

  1. var Book = function(newIsbn, newTitle, newAuthor) { // implements Publication
  2. // Private attributes.
  3. var isbn, title, author;
  4. // Private method.
  5. function checkIsbn(isbn) { //在方法内定义方法来隐藏
  6. ...
  7. }
  8. // Privileged methods.
  9. this.getIsbn = function() {
  10. return isbn;
  11. };
  12. this.setIsbn = function(newIsbn) {
  13. if(!checkIsbn(newIsbn)) throw new Error('Book: Invalid ISBN.');
  14. isbn = newIsbn;
  15. };
  16. this.getTitle = function() {
  17. return title;
  18. };
  19. this.setTitle = function(newTitle) {
  20. title = newTitle || 'No title specified';
  21. };
  22. this.getAuthor = function() {
  23. return author;
  24. };
  25. this.setAuthor = function(newAuthor) {
  26. author = newAuthor || 'No author specified';
  27. };
  28. // Constructor code.
  29. this.setIsbn(newIsbn);
  30. this.setTitle(newTitle);
  31. this.setAuthor(newAuthor);
  32. };
  33. // Public, non-privileged methods.
  34. Book.prototype = {
  35. display: function() {
  36. ...
  37. }
  38. };

常量 Constants

Class.getUPPER_BOUND();

var Class = (function() {
// Constants (created as private static attributes).
var UPPER_BOUND = 100;
// Privileged static method.
this.getUPPER_BOUND() {
return UPPER_BOUND;
}
...
// Return the constructor.
return function(constructorArgument) {
...
}
})();

 

为了避免类的完整和独立,使用上述方法。!!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值