| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,14 +1,14 @@ | |||
| 1 | 1 | // Accessor without function callbacks: returning requested data in accessors. | |
| 2 | 2 | window.API = window.API || {}; | |
| 3 | 3 | API.prototype = function() { | |
| 4 | - var name = 'Hello world'; | ||
| 4 | + var name ='Hello world'; | ||
| 5 | 5 | // Privileged mutator method. | |
| 6 | - setName: function(newName) { | ||
| 6 | + this.setName= function (newName) { | ||
| 7 | 7 | name = newName; | |
| 8 | 8 | return this; | |
| 9 | - }, | ||
| 9 | + }; | ||
| 10 | 10 | // Privileged accessor method. | |
| 11 | - getName: function() { | ||
| 11 | + this.getName = function() { | ||
| 12 | 12 | return name; | |
| 13 | 13 | } | |
| 14 | 14 | }(); | |
@@ -23,12 +23,12 @@ window.API2 = window.API2 || {}; | |||
| 23 | 23 | API2.prototype = function() { | |
| 24 | 24 | var name = 'Hello world'; | |
| 25 | 25 | // Privileged mutator method. | |
| 26 | - setName: function(newName) { | ||
| 26 | + this.setName= function(newName) { | ||
| 27 | 27 | name = newName; | |
| 28 | 28 | return this; | |
| 29 | 29 | }, | |
| 30 | 30 | // Privileged accessor method. | |
| 31 | - getName: function(callback) { | ||
| 31 | + this.getName=function(callback) { | ||
| 32 | 32 | callback.call(this, name); | |
| 33 | 33 | return this; | |
| 34 | 34 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,10 @@ | |||
| 1 | + 方法的链式调用<br /> | ||
| 2 | + Javascript中的对象是作为引用被传递的,所以可以让每个方法都传回对象的引用,即返回this值;<br /> | ||
| 3 | + 这种编程风格可以简化代码的编写工作,让代码更简洁易读,<br /> | ||
| 4 | + <a href="https://github.com/wchaowu/javascript-code/blob/master/JavaScript-Design-Patterns/Chaining/3-Building%20a%20chainable%20JavaScript%20library.js">示例程序</a><br /> | ||
| 5 | + <br /> | ||
| 6 | + 为了解决命名冲突,使用指定的API,可以为框架提供一个安装器<br /> | ||
| 7 | + <a href="https://github.com/wchaowu/javascript-code/blob/master/JavaScript-Design-Patterns/Chaining/3-The%20structure%20of%20the%20chain.js">示例程序</a><br /> | ||
| 8 | + <br /> | ||
| 9 | + 如果想让getter和setter方法都支持链式调用,可以在getter方法中使用回调技术, | ||
| 10 | + <a href="https://github.com/wchaowu/javascript-code/blob/master/JavaScript-Design-Patterns/Chaining/4-Using%20callbacks.js">示例程序</a> | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -47,3 +47,6 @@ github:<a href="https://github.com/wchaowu/Javascript-Design-Patterns"> https:// | |||
| 47 | 47 | 参考资料 | |
| 48 | 48 | ||
| 49 | 49 | <a href="http://www.apress.com/">http://www.apress.com/</a> | |
| 50 | + | ||
| 51 | + 学习资料 | ||
| 52 | + <a href="http://www.alloyteam.com/2012/10/common-javascript-design-patterns/">alloyteam</a> | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments