Skip to content

Commit b2c7d3d

Browse files
committed
don't overwrite the prototype
1 parent 4c04d5c commit b2c7d3d

File tree

1 file changed

+34
-41
lines changed

1 file changed

+34
-41
lines changed

README.md

Lines changed: 34 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -984,14 +984,12 @@
984984
this.set('lightsaber', lightsaber);
985985
}
986986
987-
Jedi.prototype = {
988-
set: function(key, val) {
989-
this[key] = val;
990-
},
987+
Jedi.prototype.set: function(key, val) {
988+
this[key] = val;
989+
};
991990
992-
get: function(key) {
993-
return this[key];
994-
}
991+
Jedi.prototype.get = function(key) {
992+
return this[key];
995993
};
996994
```
997995
@@ -1000,23 +998,14 @@
1000998
1001999
## <a name='constructors'>Constructors</a>
10021000
1003-
- Assign the constructors prototype as an object. This saves us some typing. Also, the extra level of indentation acts as an indication that you are working on the prototype and not instance methods.
1001+
- Assign methods to the prototype object, instead of overwriting the prototype with a new object. Overwriting the prototype makes inheritance impossible: by resetting the prototype you'll overwrite the base!
10041002

10051003
```javascript
10061004
function Jedi() {
10071005
console.log('new jedi');
10081006
}
10091007
10101008
// bad
1011-
Jedi.prototype.fight = function fight() {
1012-
console.log('fighting');
1013-
};
1014-
1015-
Jedi.prototype.block = function block() {
1016-
console.log('blocking');
1017-
};
1018-
1019-
// good
10201009
Jedi.prototype = {
10211010
fight: function fight() {
10221011
console.log('fighting');
@@ -1026,38 +1015,43 @@
10261015
console.log('blocking');
10271016
}
10281017
};
1018+
1019+
// good
1020+
Jedi.prototype.fight = function fight() {
1021+
console.log('fighting');
1022+
};
1023+
1024+
Jedi.prototype.block = function block() {
1025+
console.log('blocking');
1026+
};
10291027
```
10301028

10311029
- Constructor methods should try to return `this`. This helps with method chaining which is often useful.
10321030

10331031
```javascript
10341032
// bad
1035-
Jedi.prototype = {
1036-
jump: function() {
1037-
this.jumping = true;
1038-
return true;
1039-
},
1033+
Jedi.prototype.jump = function() {
1034+
this.jumping = true;
1035+
return true;
1036+
};
10401037
1041-
setHeight: function(height) {
1042-
this.height = height;
1043-
}
1038+
Jedi.prototype.setHeight = function(height) {
1039+
this.height = height;
10441040
};
10451041
10461042
var luke = new Jedi();
10471043
luke.jump(); // => true
10481044
luke.setHeight(20) // => undefined
10491045
10501046
// good
1051-
Jedi.prototype = {
1052-
jump: function() {
1053-
this.jumping = true;
1054-
return this;
1055-
},
1047+
Jedi.prototype.jump = function() {
1048+
this.jumping = true;
1049+
return this;
1050+
};
10561051
1057-
setHeight: function(height) {
1058-
this.height = height;
1059-
return this;
1060-
}
1052+
Jedi.prototype.setHeight = function(height) {
1053+
this.height = height;
1054+
return this;
10611055
};
10621056
10631057
var luke = new Jedi();
@@ -1075,14 +1069,12 @@
10751069
this.name = options.name || 'no name';
10761070
}
10771071
1078-
Jedi.prototype = {
1079-
getName: function getName() {
1080-
return this.name;
1081-
},
1072+
Jedi.prototype.getName = function getName() {
1073+
return this.name;
1074+
};
10821075
1083-
toString: function toString() {
1084-
return 'Jedi - ' + this.getName();
1085-
}
1076+
Jedi.prototype.toString = function toString() {
1077+
return 'Jedi - ' + this.getName();
10861078
};
10871079
```
10881080
@@ -1254,6 +1246,7 @@
12541246
## <a name='contributors'>Contributors</a>
12551247

12561248
- [Dave Augustine](//github.com/daveaugustine) || [@daveaugustine](//twitter.com/daveaugustine)
1249+
- [Matt Baker](//github.com/reissbaker) || (@reissbaker)(//twitter/reissbaker)
12571250

12581251
## <a name='license'>License</a>
12591252

0 commit comments

Comments
 (0)