You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: readme.md
+22-20Lines changed: 22 additions & 20 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -42,7 +42,7 @@ In the repo you will find many examples of code which will have the same functio
42
42
###giraffeMaker
43
43
44
44
```javascript
45
-
var giraffeMaker = (name, height){
45
+
var giraffeMaker = (name, height){
46
46
var giraffe = {};
47
47
giraffe.name= name;
48
48
giraffe.height= height;
@@ -57,7 +57,7 @@ This piece of code creates a new object and sets properties upon it, then return
57
57
### greet();
58
58
59
59
```javascript
60
-
vargreet=function(){
60
+
vargreet=function(){
61
61
console.log('Hello, my name is '+this.name+', it is nice to meet you.');
62
62
};
63
63
```
@@ -67,9 +67,9 @@ This is the greet function, it logs a string to console and wants to refer to th
67
67
### eat();
68
68
69
69
```javascript
70
-
vareat=function(){
71
-
if(this.height>2){
72
-
if(this.hunger>0){
70
+
vareat=function(){
71
+
if(this.height>2){
72
+
if(this.hunger>0){
73
73
this.hunger-=this.height;
74
74
} else {
75
75
console.log(this.name+" is not hungry.");
@@ -91,10 +91,10 @@ The Journey:
91
91
The first step will be creating some sort of maker function.
92
92
93
93
```javascript
94
-
vartheMaker=function(value){
94
+
vartheMaker=function(value){
95
95
var theThingToBeMade = {};
96
96
theThingToBeMade.ownValue= value;
97
-
theThingToBeMade.shout=function(){
97
+
theThingToBeMade.shout=function(){
98
98
console.log("I have my own Value! Let it be known as "+theThingToBeMade.ownValue+"!");
99
99
};
100
100
@@ -128,15 +128,15 @@ You can think about a Class as a mechanism which allows you to create objects wh
128
128
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?
129
129
130
130
```javascript
131
-
vartheMaker=function(value){
131
+
vartheMaker=function(value){
132
132
var theThingToBeMade = {};
133
133
theThingToBeMade.ownValue= value;
134
134
theThingToBeMade.shout= shout;
135
135
136
136
return theThingToBeMade;
137
137
};
138
138
139
-
varshout=function(){
139
+
varshout=function(){
140
140
console.log("I have my own Value! Let it be known as "+this.ownValue+"!");
141
141
};
142
142
```
@@ -147,7 +147,7 @@ In this snippet of code, we take the shout functionality out of the maker functi
147
147
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.
148
148
149
149
```javascript
150
-
varmaker=function(value){
150
+
varmaker=function(value){
151
151
theThingToBeMade = {};
152
152
theThingToBeMade.ownValue= value;
153
153
//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
185
185
1. It refers to what is to the left of the dot at calltime.
186
186
2. It's magical.
187
187
188
-
shout = function(){
188
+
```javascript
189
+
shout=function() {
189
190
console.log(this.ownValue);
190
191
};
191
192
192
193
when you run newThing.shout(), this refers to newThing, as it is to the left of the dot at calltime.
193
194
- JavaScript would interpret this instance of the function call like:
194
195
195
-
shout = function(){
196
+
shout = function(){
196
197
console.log(newThing.ownValue);
197
198
};
198
199
199
200
when you run thatOtherThing.shout(), this refers to thatOtherThing.
200
201
201
202
thatOtherThing.shout(); // "I might not be the thing you wanted."
202
203
newThing.shout(); // "I am a thing!"
204
+
```
203
205
204
206
Using the keyword this allows us to refer to the particular instance of the class that we intend to within the shared function.
205
207
206
208
###Sharing functions using extend() - step 2:
207
209
208
210
```javascript
209
-
varextend=function(copyTo, copyFrom){
210
-
for (var property in copyFrom){
211
+
varextend=function(copyTo, copyFrom){
212
+
for (var property in copyFrom){
211
213
copyTo[property] = copyFrom[property];
212
214
}
213
215
};
@@ -219,7 +221,7 @@ The extend function copies every property from one object onto another. This wil
219
221
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.
220
222
221
223
```javascript
222
-
varmaker=function(value){
224
+
varmaker=function(value){
223
225
var thingToBeMade =Object.create(maker.stuffAllThingsShouldHave);
224
226
thingToBeMade.ownValue= value;
225
227
@@ -238,7 +240,7 @@ Object.create not only creates the new object for us, but also sets up delegatio
238
240
```javascript
239
241
240
242
maker.stuffAllThingsShouldHave= {
241
-
shout:function(){console.log(this.ownValue);}
243
+
shout:function(){console.log(this.ownValue);}
242
244
};
243
245
```
244
246
@@ -269,11 +271,11 @@ The function following 'new', is run in "constructor mode", these rules apply:
269
271
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.
0 commit comments