Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
raganwald committed Jan 5, 2014
1 parent fe04148 commit 6762cd1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 25 deletions.
2 changes: 1 addition & 1 deletion manuscript/markdown/Functions/summary.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ T> * Functions are *reference values*.
T> * Functions are applied to arguments.
T> * The arguments are passed by sharing, which is also called "pass by value."
T> * Function bodies have zero or more expressions.
T> * Function application evaluates to the value of the last expression evaluated or `undefined`.
T> * Function application evaluates whatever is returned with the `return` keyword, or to `undefined`.
T> * Function application creates a scope. Scopes are nested and free variable references closed over.
T> * Variables can shadow variables in an enclosing scope.
T> * `let` is an idiom where we create a function and call it immediately in order to bind values to names.
Expand Down
48 changes: 24 additions & 24 deletions manuscript/markdown/Instances and Classes/extends.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ Here's our `Queue`:
tail: -1
})
};

extend(Queue.prototype, {
pushTail: function (value) {
return this.array[this.tail += 1] = value
},
pullHead: function () {
var value;

if (!this.isEmpty()) {
value = this.array[this.head]
this.array[this.head] = void 0;
Expand All @@ -28,24 +28,24 @@ Here's our `Queue`:
},
isEmpty: function () {
return this.tail < this.head
}
}
});

And here's what our `Deque` would look like before we wire things together:

var Dequeue = function () {
Queue.prototype.constructor.call(this)
};

Dequeue.INCREMENT = 4;

extend(Dequeue.prototype, {
size: function () {
return this.tail - this.head + 1
},
pullTail: function () {
var value;

if (!this.isEmpty()) {
value = this.array[this.tail];
this.array[this.tail] = void 0;
Expand All @@ -55,7 +55,7 @@ And here's what our `Deque` would look like before we wire things together:
},
pushHead: function (value) {
var i;

if (this.head === 0) {
for (i = this.tail; i >= this.head; --i) {
this.array[i + INCREMENT] = this.array[i]
Expand All @@ -78,26 +78,26 @@ Yes they can. Imagine that `Deque.prototype` was a proxy for an instance of `Que
Here's such a proxy:

var QueueProxy = function () {}

QueueProxy.prototype = Queue.prototype

Our `QueueProxy` isn't actually a `Queue`, but its `prototype` is an alias of `Queue.prototype`. Thus, it can pick up `Queue`'s behaviour. We want to use it for our `Deque`'s prototype. Let's insert that code in our class definition:

var Dequeue = function () {
Queue.prototype.constructor.call(this)
};

Dequeue.INCREMENT = 4;

Dequeue.prototype = new QueueProxy();

extend(Dequeue.prototype, {
size: function () {
return this.tail - this.head + 1
},
pullTail: function () {
var value;

if (!this.isEmpty()) {
value = this.array[this.tail];
this.array[this.tail] = void 0;
Expand All @@ -107,7 +107,7 @@ Our `QueueProxy` isn't actually a `Queue`, but its `prototype` is an alias of `Q
},
pushHead: function (value) {
var i;

if (this.head === 0) {
for (i = this.tail; i >= this.head; --i) {
this.array[i + INCREMENT] = this.array[i]
Expand All @@ -118,7 +118,7 @@ Our `QueueProxy` isn't actually a `Queue`, but its `prototype` is an alias of `Q
this.array[this.head -= 1] = value
}
});

And it seems to work:

d = new Dequeue()
Expand All @@ -131,23 +131,23 @@ And it seems to work:
//=> '!'
d.pullHead()
//=> 'JavaScript'

Wonderful!

### getting the constructor element right

How about some of the other things we've come to expect from instances?

d.constructor == Dequeue
//=> false

Oops! Messing around with Dequeue's prototype broke this important equivalence. Luckily for us, the `constructor` property is mutable for objects we create. So, let's make a small change to `QueueProxy`:

var QueueProxy = function () {
this.constructor = Dequeue;
}
QueueProxy.prototype = Queue.prototype

Repeat. Now it works:

d.constructor === Dequeue
Expand All @@ -173,16 +173,16 @@ And use it in `Dequeue`:
var Dequeue = child(Queue, function () {
Queue.prototype.constructor.call(this)
});

Dequeue.INCREMENT = 4;

extend(Dequeue.prototype, {
size: function () {
return this.tail - this.head + 1
},
pullTail: function () {
var value;

if (!this.isEmpty()) {
value = this.array[this.tail];
this.array[this.tail] = void 0;
Expand All @@ -192,7 +192,7 @@ And use it in `Dequeue`:
},
pushHead: function (value) {
var i;

if (this.head === 0) {
for (i = this.tail; i >= this.head; --i) {
this.array[i + INCREMENT] = this.array[i]
Expand All @@ -210,4 +210,4 @@ Some folks just **love** to build their own mechanisms. When all goes well, they

If you're keen on learning, you can work on improving the above code to handle extending constructor properties, automatically calling the parent constructor function, and so forth. Or you can decide that doing it by hand isn't that hard so why bother putting a thin wrapper around it?

It's up to you, while JavaScript isn't the tersest language, it isn't so baroque that building inheritence ontologies requires hundreds of lines of inscrutable code.
It's up to you, while JavaScript isn't the tersest language, it isn't so baroque that building inheritance ontologies requires hundreds of lines of inscrutable code.

0 comments on commit 6762cd1

Please sign in to comment.