Skip to content

Commit a87d7dc

Browse files
committed
Restore order to readme
1 parent 7af3b78 commit a87d7dc

File tree

1 file changed

+22
-20
lines changed

1 file changed

+22
-20
lines changed

readme.md

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ In the repo you will find many examples of code which will have the same functio
4242
###giraffeMaker
4343

4444
```javascript
45-
var giraffeMaker = (name, height){
45+
var giraffeMaker = (name, height) {
4646
var giraffe = {};
4747
giraffe.name = name;
4848
giraffe.height = height;
@@ -57,7 +57,7 @@ This piece of code creates a new object and sets properties upon it, then return
5757
### greet();
5858

5959
```javascript
60-
var greet = function(){
60+
var greet = function() {
6161
console.log('Hello, my name is ' + this.name + ', it is nice to meet you.');
6262
};
6363
```
@@ -67,9 +67,9 @@ This is the greet function, it logs a string to console and wants to refer to th
6767
### eat();
6868

6969
```javascript
70-
var eat = function(){
71-
if(this.height > 2){
72-
if(this.hunger > 0){
70+
var eat = function() {
71+
if(this.height > 2) {
72+
if(this.hunger > 0) {
7373
this.hunger -= this.height;
7474
} else {
7575
console.log(this.name + " is not hungry.");
@@ -91,10 +91,10 @@ The Journey:
9191
The first step will be creating some sort of maker function.
9292

9393
```javascript
94-
var theMaker = function(value){
94+
var theMaker = function(value) {
9595
var theThingToBeMade = {};
9696
theThingToBeMade.ownValue = value;
97-
theThingToBeMade.shout = function(){
97+
theThingToBeMade.shout = function() {
9898
console.log("I have my own Value! Let it be known as " + theThingToBeMade.ownValue + "!");
9999
};
100100

@@ -128,15 +128,15 @@ You can think about a Class as a mechanism which allows you to create objects wh
128128
The code in the previous maker function creates a new method .shout() for each thing that it creates, attaching this new function to each instance. What are we really trying to do here? Do we want each thing to have a new method of its own? Wouldn't it be cool if they could just share one function which they can inherit from being a thing?
129129

130130
```javascript
131-
var theMaker = function(value){
131+
var theMaker = function(value) {
132132
var theThingToBeMade = {};
133133
theThingToBeMade.ownValue = value;
134134
theThingToBeMade.shout = shout;
135135

136136
return theThingToBeMade;
137137
};
138138

139-
var shout = function(){
139+
var shout = function() {
140140
console.log("I have my own Value! Let it be known as " + this.ownValue + "!");
141141
};
142142
```
@@ -147,7 +147,7 @@ In this snippet of code, we take the shout functionality out of the maker functi
147147
If we move the functionality outside of the maker function, we lose our previous way of referring to the created object, which was something like this.
148148

149149
```javascript
150-
var maker = function(value){
150+
var maker = function(value) {
151151
theThingToBeMade = {};
152152
theThingToBeMade.ownValue = value;
153153
//This assigns a property ownValue to the object. We can refer to it within the two brackets that wrap the maker function.
@@ -185,29 +185,31 @@ When asking about how the keyword 'this' works you will usually find one of two
185185
1. It refers to what is to the left of the dot at calltime.
186186
2. It's magical.
187187

188-
shout = function(){
188+
```javascript
189+
shout = function() {
189190
console.log(this.ownValue);
190191
};
191192

192193
when you run newThing.shout(), this refers to newThing, as it is to the left of the dot at calltime.
193194
- JavaScript would interpret this instance of the function call like:
194195

195-
shout = function(){
196+
shout = function() {
196197
console.log(newThing.ownValue);
197198
};
198199

199200
when you run thatOtherThing.shout(), this refers to thatOtherThing.
200201

201202
thatOtherThing.shout(); // "I might not be the thing you wanted."
202203
newThing.shout(); // "I am a thing!"
204+
```
203205

204206
Using the keyword this allows us to refer to the particular instance of the class that we intend to within the shared function.
205207

206208
###Sharing functions using extend() - step 2:
207209

208210
```javascript
209-
var extend = function(copyTo, copyFrom){
210-
for (var property in copyFrom){
211+
var extend = function(copyTo, copyFrom) {
212+
for (var property in copyFrom) {
211213
copyTo[property] = copyFrom[property];
212214
}
213215
};
@@ -219,7 +221,7 @@ The extend function copies every property from one object onto another. This wil
219221
A prototype allows you to share methods and properties among class members. How this works more precisely is that once you set up a prototype chain, or delegate to a prototype. Any failed lookup on an object will be delegated to its prototype which will be checked for what was looked up on the object. The idea of using a prototype is to have any shared properties or methods on the prototype, allowing all class members to use them. The only property that should stay in the maker function would be one that changes or has a specific value for each instance.
220222

221223
```javascript
222-
var maker = function(value){
224+
var maker = function(value) {
223225
var thingToBeMade = Object.create(maker.stuffAllThingsShouldHave);
224226
thingToBeMade.ownValue = value;
225227

@@ -238,7 +240,7 @@ Object.create not only creates the new object for us, but also sets up delegatio
238240
```javascript
239241

240242
maker.stuffAllThingsShouldHave = {
241-
shout: function(){console.log(this.ownValue);}
243+
shout: function() {console.log(this.ownValue);}
242244
};
243245
```
244246

@@ -269,11 +271,11 @@ The function following 'new', is run in "constructor mode", these rules apply:
269271
You would use 'new' when creating another instance of the class. When creating objects using Pseudo-Classical we need to use a maker function which has more specific rules.
270272

271273
```javascript
272-
var Thing = function(value){
274+
var Thing = function(value) {
273275
this.ownValue = value;
274276
};
275277

276-
Thing.prototype.shout = function(){console.log(this.ownValue);};
278+
Thing.prototype.shout = function() {console.log(this.ownValue);};
277279

278280
var newThing = new Thing(1);
279281
var otherThing = new Thing(100);
@@ -284,7 +286,7 @@ The maker function we use in Pseudo-Classical does not need to create, return th
284286
Here is how the interpreter sees the same function as it is run in constructor mode:
285287

286288
```javascript
287-
var Thing = function(value){
289+
var Thing = function(value) {
288290
var newThing = Object.create(Thing.prototype);
289291
this = newThing;
290292
this.ownValue = value;
@@ -300,7 +302,7 @@ Functions are only run in constructor mode when you invoke the keyword new. It i
300302
```javascript
301303
var protoThing = Thing(10);
302304

303-
protoThing = function(10){
305+
protoThing = function(10) {
304306
this.ownValue = 10;
305307
};
306308
```

0 commit comments

Comments
 (0)