Skip to content

Instantly share code, notes, and snippets.

View specialunderwear's full-sized avatar

Voxin Muyli specialunderwear

View GitHub Profile
@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 / 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 / simplercounterexample.js
Created January 30, 2012 00:22
simple counter example
> var count = Counter;
> count;
0
> count.next()
1
@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 / 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 / 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 / localscope.js
Created January 30, 2012 00:05
no new scope
> var base = change_this_one(10);
> one;
10
> 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 / threeconstructor.js
Created January 29, 2012 23:57
three as a constructor
> var three = new next(2);
> three;
3
@specialunderwear
specialunderwear / newfunction.js
Created January 29, 2012 23:46
next function
> var next = function(input) {
> return input + 1;
> }
> var three = new next;
> three;
[Function]
> three(2);
3