|
984 | 984 | this.set('lightsaber', lightsaber); |
985 | 985 | } |
986 | 986 |
|
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 | + }; |
991 | 990 |
|
992 | | - get: function(key) { |
993 | | - return this[key]; |
994 | | - } |
| 991 | + Jedi.prototype.get = function(key) { |
| 992 | + return this[key]; |
995 | 993 | }; |
996 | 994 | ``` |
997 | 995 |
|
|
1000 | 998 |
|
1001 | 999 | ## <a name='constructors'>Constructors</a> |
1002 | 1000 |
|
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! |
1004 | 1002 |
|
1005 | 1003 | ```javascript |
1006 | 1004 | function Jedi() { |
1007 | 1005 | console.log('new jedi'); |
1008 | 1006 | } |
1009 | 1007 |
|
1010 | 1008 | // 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 |
1020 | 1009 | Jedi.prototype = { |
1021 | 1010 | fight: function fight() { |
1022 | 1011 | console.log('fighting'); |
|
1026 | 1015 | console.log('blocking'); |
1027 | 1016 | } |
1028 | 1017 | }; |
| 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 | + }; |
1029 | 1027 | ``` |
1030 | 1028 |
|
1031 | 1029 | - Constructor methods should try to return `this`. This helps with method chaining which is often useful. |
1032 | 1030 |
|
1033 | 1031 | ```javascript |
1034 | 1032 | // 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 | + }; |
1040 | 1037 |
|
1041 | | - setHeight: function(height) { |
1042 | | - this.height = height; |
1043 | | - } |
| 1038 | + Jedi.prototype.setHeight = function(height) { |
| 1039 | + this.height = height; |
1044 | 1040 | }; |
1045 | 1041 |
|
1046 | 1042 | var luke = new Jedi(); |
1047 | 1043 | luke.jump(); // => true |
1048 | 1044 | luke.setHeight(20) // => undefined |
1049 | 1045 |
|
1050 | 1046 | // 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 | + }; |
1056 | 1051 |
|
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; |
1061 | 1055 | }; |
1062 | 1056 |
|
1063 | 1057 | var luke = new Jedi(); |
|
1075 | 1069 | this.name = options.name || 'no name'; |
1076 | 1070 | } |
1077 | 1071 |
|
1078 | | - Jedi.prototype = { |
1079 | | - getName: function getName() { |
1080 | | - return this.name; |
1081 | | - }, |
| 1072 | + Jedi.prototype.getName = function getName() { |
| 1073 | + return this.name; |
| 1074 | + }; |
1082 | 1075 |
|
1083 | | - toString: function toString() { |
1084 | | - return 'Jedi - ' + this.getName(); |
1085 | | - } |
| 1076 | + Jedi.prototype.toString = function toString() { |
| 1077 | + return 'Jedi - ' + this.getName(); |
1086 | 1078 | }; |
1087 | 1079 | ``` |
1088 | 1080 |
|
|
1254 | 1246 | ## <a name='contributors'>Contributors</a> |
1255 | 1247 |
|
1256 | 1248 | - [Dave Augustine](//github.com/daveaugustine) || [@daveaugustine](//twitter.com/daveaugustine) |
| 1249 | + - [Matt Baker](//github.com/reissbaker) || (@reissbaker)(//twitter/reissbaker) |
1257 | 1250 |
|
1258 | 1251 | ## <a name='license'>License</a> |
1259 | 1252 |
|
|
0 commit comments