Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit 85605e4c2424e4102b9eba05fd9eea0dc24ff6d0 @mattpodwysocki committed Dec 12, 2011
@@ -0,0 +1,24 @@
+<!DOCTYPE html>
+<head>
+
+</head>
+<body>
+ <div id="results"></div>
+
+ <script type="text/javascript">
+ (function() {
+ function write(message) {
+ document.getElementById('results').innerHTML += message + '<br />';
+ }
+
+ var name = 'Matthew';
+ write('Type: ' + typeof name + ' Value: ' + name);
+
+ var number = 1234;
+ write('Type: ' + typeof number + ' Value: ' + number);
+
+ write('write type: ' + typeof write);
+ })();
+ </script>
+</body>
+</html>
@@ -0,0 +1,72 @@
+<!DOCTYPE html>
+<head>
+
+</head>
+<body>
+ <div id="results"></div>
+
+ <script type="text/javascript">
+ // Immediately execute me with renames of globals
+ (function(global, document) {
+ function write(message) {
+ document.getElementById('results').innerHTML += message + '<br />';
+ }
+
+ function add(x, y) {
+ return x + y;
+ }
+
+ var added1 = add(3, 4); // Normal invocation
+ write('Normal added: ' + added1);
+
+ var args = [3, 4];
+ var added2 = add.apply(/* this */ null, args);
+ write('Apply added: ' + added2);
+
+ var added3 = add.call(/* this */ null, 3, 4);
+ write('Call added: ' + added2);
+
+ // Make the 'this' call more meaningful
+ var logger = {
+ log: function (message) {
+ write(message);
+ }
+ };
+
+ function loggingAdd(x, y) {
+ // Assume there is a logger somewhere
+ var result = x + y;
+ this.log('Result: ' + result);
+ return result;
+ };
+
+ var added4 = loggingAdd.apply(/* this */ logger, [5, 6]);
+ write('Apply added with logging: ' + added4);
+
+ var added5 = loggingAdd.call(/* this */ logger, 5, 6);
+ write('Call added with logging: ' + added5);
+
+ // Mixins
+ var Person = function (firstName, lastName) {
+ this.firstName = firstName;
+ this.lastName = lastName;
+ };
+
+ Person.prototype.getFullName = function() {
+ return this.firstName + ' ' + this.lastName;
+ };
+
+ var p = new Person('Bill', 'Gates');
+ write('Full Name: ' + p.getFullName());
+
+ var me = {
+ firstName: 'Matthew',
+ lastName: 'Podwysocki'
+ };
+
+ var fullName = Person.prototype.getFullName.apply(me);
+ write('Full Name with apply: ' + fullName);
+ })(this, document);
+ </script>
+</body>
+</html>
@@ -0,0 +1,124 @@
+<!DOCTYPE html>
+<head>
+
+</head>
+<body>
+ <div id="results"></div>
+ <script type="text/javascript">
+ // Polyfill Array.reduce
+ if ( !Array.prototype.reduce ) {
+ Array.prototype.reduce = function reduce(accumulator){
+ var i, l = this.length, curr;
+
+ if(typeof accumulator !== "function") // ES5 : "If IsCallable(callbackfn) is false, throw a TypeError exception."
+ throw new TypeError("First argument is not callable");
+
+ if((l == 0 || l === null) && (arguments.length <= 1))// == on purpose to test 0 and false.
+ throw new TypeError("Array length is 0 and no second argument");
+
+ if(arguments.length <= 1){
+ curr = this[0]; // Increase i to start searching the secondly defined element in the array
+ i = 1; // start accumulating at the second element
+ }
+ else{
+ curr = arguments[1];
+ }
+
+ for(i = i || 0 ; i < l ; ++i){
+ if(i in this)
+ curr = accumulator.call(undefined, curr, this[i], i, this);
+ }
+
+ return curr;
+ };
+ }
+ </script>
+ <script type="text/javascript">
+ // Immediately execute me with renames of globals
+ (function(global, document) {
+ function write(message) {
+ document.getElementById('results').innerHTML += message + '<br />';
+ }
+
+ // Static number of arguments
+ var add = function (x, y) {
+ return x + y;
+ };
+
+ write('Add number of arguments: ' + add(3, 4));
+ write('Add too few arguments: ' + add(3));
+ write('Add too many arguments: ' + add(3, 4, 5));
+
+ // Overloading add
+ add = function (x, y) {
+ var stringify = function (s) {
+ return typeof s === 'string' ? global.parseInt(s) : s;
+ };
+ x = stringify(x);
+ y = stringify(y);
+ return x + y;
+ };
+
+ write('Add with integers: ' + add(3, 4));
+ write('Add with string and int: ' + add('3', 4));
+ write('Add with strings: ' + add('3', '4'));
+
+ // Iterating through arguments, remember this isn't a normal array
+ add = function () {
+ var sum = 0, i;
+ write('Arguments length: ' + arguments.length);
+ for (i = 0; i < arguments.length; i++) {
+ sum += arguments[i];
+ }
+
+ return sum;
+ };
+
+ var add1 = add(1,2,3,4);
+ write('Iterating add: ' + add1);
+
+ // But we can make it an array via Array.prototype.slice.call
+ var slice = Array.prototype.slice;
+ add = function () {
+ var args = slice.call(arguments);
+ write('Arguments: ' + args);
+ var args2 = slice.call(arguments, 1);
+ write('Arguments skipping 1: ' + args2);
+
+ // Using ES5 Array extras to calculate
+ return args.reduce(function (acc, item) {
+ return acc + item;
+ });
+ };
+
+ var add2 = add(5,6,7,8);
+ write('Functional add: ' + add2);
+
+ // Default arguments
+ var defaultComparer = function(x, y) { return x === y; };
+ Array.prototype.contains = function(value, comparer) {
+ comparer || (comparer = defaultComparer);
+ var i, length = this.length;
+ for (i = 0; i < length; i++) {
+ if (comparer(value, this[i])) {
+ return true;
+ }
+ }
+ return false;
+ };
+
+ var arr1 = [1,2,3];
+ write('Contains 3 with default comparer: ' + arr1.contains(3));
+
+ var arr2 = [
+ { name: 'Larry' },
+ { name: 'Curly' },
+ { name: 'Moe' }
+ ];
+ var comparer = function(x, y) { return x === y.name; };
+ write('Contains with comparer and Larry: ' + arr2.contains('Larry', comparer));
+
+ })(this, document);
+ </script>
+</body>
+</html>
@@ -0,0 +1,40 @@
+<!DOCTYPE html>
+<head>
+
+</head>
+<body>
+ <div id="results"></div>
+ <script type="text/javascript">
+
+ </script>
+ <script type="text/javascript">
+ // Immediately execute me with renames of globals
+ (function(global, document) {
+ function write(message) {
+ document.getElementById('results').innerHTML += message + '<br />';
+ }
+
+ String.prototype.capitalCase = function () {
+ return this.charAt(0).toUpperCase() + this.slice(1);
+ };
+
+ write('Capitalized: ' + 'foobar'.capitalCase());
+
+ // Extend the Function
+ Function.prototype.method = function (name, fn) {
+ if (!this.prototype[name]) {
+ this.prototype[name] = fn;
+ return this;
+ }
+ };
+
+ Number.method('integer', function () {
+ return Math[this < 0 ? 'ceil' : 'floor'](this);
+ });
+
+ var i = (-20 / 3).integer();
+ write('Rounded: ' + i);
+ })(this, document);
+ </script>
+</body>
+</html>
@@ -0,0 +1,40 @@
+<!DOCTYPE html>
+<head>
+
+</head>
+<body>
+ <div id="results"></div>
+ <script type="text/javascript">
+
+ </script>
+ <script type="text/javascript">
+ // Immediately execute me with renames of globals
+ (function(global, document) {
+ function write(message) {
+ document.getElementById('results').innerHTML += message + '<br />';
+ }
+
+ var factorial = function factorial(x) {
+ if (x < 2) {
+ return 1;
+ }
+ return x * factorial(x - 1);
+ };
+
+ // Non-tail recursive factorial
+ write('Non-tail recursive factorial: ' + factorial(10));
+
+ factorial = function factorial (x, acc) {
+ acc = acc || 1;
+ if (x < 2) {
+ return acc;
+ }
+ return factorial(x - 1, acc * x);
+ };
+
+ // tail recursive factorial
+ write('Tail recursive factorial: ' + factorial(10));
+ })(this, document);
+ </script>
+</body>
+</html>
@@ -0,0 +1,81 @@
+<!DOCTYPE html>
+<head>
+
+</head>
+<body>
+ <div id="results"></div>
+ <script type="text/javascript">
+
+ </script>
+ <script type="text/javascript">
+ // Immediately execute me with renames of globals
+ (function(global, document) {
+ function write(message) {
+ document.getElementById('results').innerHTML += message + '<br />';
+ }
+
+ // Scope of value trapped
+ var counter = (function () {
+ var value = 0;
+ return {
+ getValue : function() { return value; },
+ increment: function(inc) {
+ value += typeof inc === 'number' ? inc : 1;
+ }
+ };
+ })();
+
+ write('Scoped initial: ' + counter.getValue());
+ counter.increment(3);
+ write('Scoped increment by 3: ' + counter.getValue());
+ write('Count defined? ' + (counter.count !== undefined));
+
+ // Fade DOM
+ var fade = function (node) {
+ var level = 1;
+ var step = function() {
+ var hex = level.toString(16);
+ node.style.backgroundColor = '#FFFF' + hex + hex;
+ if (level++ < 15) {
+ setTimeout(step, 100);
+ }
+ };
+ setTimeout(step, 100);
+ };
+ fade(document.body);
+
+ // Loop and closures
+ (function () {
+ var i, arr = [];
+ for (i = 0; i < 6; i++) {
+ arr.push(function() {
+ write('Value: ' + i);
+ });
+ }
+
+ var j;
+ for (j = 0; j < arr.length; j++) {
+ arr[j]();
+ }
+ })();
+
+ // Fix the closure issue
+ (function () {
+ var i, arr = [];
+ for (i = 0; i < 6; i++) {
+ (function (i) {
+ arr.push(function() {
+ write('Fixed value: ' + i);
+ });
+ })(i);
+ }
+
+ var j;
+ for (j = 0; j < arr.length; j++) {
+ arr[j]();
+ }
+ })();
+ })(this, document, undefined);
+ </script>
+</body>
+</html>
Oops, something went wrong.

0 comments on commit 85605e4

Please sign in to comment.