Skip to content

Instantly share code, notes, and snippets.

View specialunderwear's full-sized avatar

Voxin Muyli specialunderwear

View GitHub Profile
> var one = 1;
> var change_this_one = function(baseline) {
> this.one = baseline;
> }
> one;
1
> var base = new change_this_one(10);
> one;
1
> base.one;
@specialunderwear
specialunderwear / localscope.js
Created January 30, 2012 00:05
no new scope
> var base = change_this_one(10);
> one;
10
@specialunderwear
specialunderwear / counterclass.js
Created January 30, 2012 00:11
constructor defines methods
> var Counter = function(from) {
> this.from = from;
> this.next = function() {
> this.from += 1;
> return this.from;
> };
> }
> var count_from_7 = new Counter(7);
> var count_from_3 = new Counter(3);
> count_from_7.next();
@specialunderwear
specialunderwear / nextundefined.js
Created January 30, 2012 00:14
next s not defined
> var count = new Counter;
> count.next();
Error: function next is not defined.
@specialunderwear
specialunderwear / fancycounter.js
Created January 30, 2012 00:18
simpler counter from 0
> var Counter = 0;
> Counter.next = function() {
> this += 1;
> return this;
> };
@specialunderwear
specialunderwear / simplercounterexample.js
Created January 30, 2012 00:22
simple counter example
> var count = Counter;
> count;
0
> count.next()
1
@specialunderwear
specialunderwear / modifycounter.js
Created January 30, 2012 00:27
Accidents can happen ...
> var count = Counter;
> count.next();
1
> var accidental_class_ref = Counter;
> accidental_class_ref.next();
1
> var another_counter = new Counter;
> another_counter.next();
2
@specialunderwear
specialunderwear / prototypecounter.js
Created January 30, 2012 00:31
the prototype not the value
> var Counter = 0;
> Counter.prototype.next = function() {
> this += 1;
> return this;
> };
@specialunderwear
specialunderwear / nextisprototype.js
Created January 30, 2012 00:33
next is defined only on the prototype
> var count = Counter;
> count.next();
Error: function next is not defined.
@specialunderwear
specialunderwear / changeprototype.js
Created January 30, 2012 00:44
Change prototype and existing objects change as well
> var a = Counter;
> a;
0
> a.next();
1
> a.before();
Error: function before is not defined.
> Counter.prototype.before = function() {
> return this - 1;
> };