Skip to content

Commit 9e079fe

Browse files
committed
添加详细的描述,重构代码
1 parent 75cb60b commit 9e079fe

File tree

11 files changed

+19
-16
lines changed

11 files changed

+19
-16
lines changed

JavaScript-Design-Patterns/Chaining/4-Using callbacks.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
// Accessor without function callbacks: returning requested data in accessors.
22
window.API = window.API || {};
33
API.prototype = function() {
4-
var name = 'Hello world';
4+
var name ='Hello world';
55
// Privileged mutator method.
6-
setName: function(newName) {
6+
this.setName= function (newName) {
77
name = newName;
88
return this;
9-
},
9+
};
1010
// Privileged accessor method.
11-
getName: function() {
11+
this.getName = function() {
1212
return name;
1313
}
1414
}();
@@ -23,12 +23,12 @@ window.API2 = window.API2 || {};
2323
API2.prototype = function() {
2424
var name = 'Hello world';
2525
// Privileged mutator method.
26-
setName: function(newName) {
26+
this.setName= function(newName) {
2727
name = newName;
2828
return this;
2929
},
3030
// Privileged accessor method.
31-
getName: function(callback) {
31+
this.getName=function(callback) {
3232
callback.call(this, name);
3333
return this;
3434
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff 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>

JavaScript-Design-Patterns/Chaining/REMDME.md

Lines changed: 0 additions & 10 deletions
This file was deleted.

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,6 @@ github:<a href="https://github.com/wchaowu/Javascript-Design-Patterns"> https://
4747
参考资料
4848

4949
<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>

0 commit comments

Comments
 (0)