From b0f6fe9338c5d0da445bfe7258fc0f68b54adb24 Mon Sep 17 00:00:00 2001 From: Kyle K Date: Sat, 22 Jun 2013 00:29:37 -0500 Subject: initial import --- anon-func.js | 50 ++++++++++++++++++++++++++++++++++++++++++++ closure.js | 15 ++++++++++++++ jsnotes.txt | 41 ++++++++++++++++++++++++++++++++++++ keyval-assignment.js | 25 ++++++++++++++++++++++ linkedlist.js | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++ prototype-this-new.js | 24 ++++++++++++++++++++++ 6 files changed, 212 insertions(+) create mode 100644 anon-func.js create mode 100644 closure.js create mode 100644 jsnotes.txt create mode 100644 keyval-assignment.js create mode 100644 linkedlist.js create mode 100644 prototype-this-new.js diff --git a/anon-func.js b/anon-func.js new file mode 100644 index 0000000..c4e3130 --- /dev/null +++ b/anon-func.js @@ -0,0 +1,50 @@ +/* this is an error +function() { + console.log("hello"); +} +*/ + +// current context in this outermost scope +var c = " foobar"; + +// prints nothing, code doesn't execute +(function() { + console.log('hello' + c); +}); + +// () at the end executes declared function +// NOTICE that functions are objects, better yet, first-class objects, since +// they can be assigned to a variable +(function() { + console.log('hello' + c); +})(); + +// prints hello again foobar +(function() { + console.log('hello again' + c); +}).call(this); + +// prints hello again cruel world foobar +(function(par1, par2) { + console.log('hello again' + par1 + par2 + c); +}).apply(this, [" cruel", " world"]); + +// prints 7 and 13 +var f = function() { return 7; }; +var g = (function() { return 13; }); +console.log(f()); +console.log(g()); + +(function() { + // "use strict" cannot be used since 'this' is an undeclared object, it + // exists but only for life of its scope + this.x = "barfoo"; + console.log(x); + console.log(this.x); + + (function() { + // we're lucky here since js searches for x in higher scopes + console.log(x); + console.log(x); + })(); +})(); diff --git a/closure.js b/closure.js new file mode 100644 index 0000000..9c8743c --- /dev/null +++ b/closure.js @@ -0,0 +1,15 @@ +function foo(n) { + var f = 3.14; + // closure, this anon func has access to 'n' and 'f' + // outside of scope of 'foo' + return function() { + console.log(2 * n * f); + } +} + +var bar = foo(3); +bar(); + +// notes +// as soon as n and f were referenced on line 6, they now belong to +// a closure diff --git a/jsnotes.txt b/jsnotes.txt new file mode 100644 index 0000000..d28e473 --- /dev/null +++ b/jsnotes.txt @@ -0,0 +1,41 @@ +[references] +http://msdn.microsoft.com/en-us/library/ie/d1et7k7c%28v=vs.94%29.aspx + +[data types] +- primitive + string, number boolean + +- composite (reference) + object, array + + rvalues: new Object(), Object(), function() {}, {} + rvalues: [], new Array(), Array() + +- special + null, undefined + note: null is not 0, does not have type, undefined is a string + +[anonymous functions] +wrapping code inside of 'function() { code } ()' is handy for creating a +namespace for your code and restricting scope, since js essentially has +a global and function scope, we don't want to pollute and cause collisons + +[closures] +in JavaScript, if you use the function keyword inside another function, you are +creating a closure. if a function containing function f returns that function, +that variable points to f and also to the closure, unlike C, where stack frame +would get destroyed, so f has access to variables/object in the context it was +defined in + +- what about function returning a function + that's a good way to leverage closures! + +['this' and 'prototype' object and the 'new'] +- every object has a prototype object +- the keyword 'this' refers to the object context within which the + the function is executing +- protype is used for templating, it provides access to members of an object, + when function is assigned to this object, only 1 copy exists, it is memory + efficient, also we get expected behavior when overriding that function +- 'new' changes meaning of a function, upon invocation, function is ran in blank + object context \ No newline at end of file diff --git a/keyval-assignment.js b/keyval-assignment.js new file mode 100644 index 0000000..c4b491f --- /dev/null +++ b/keyval-assignment.js @@ -0,0 +1,25 @@ +// demonstrates short hand way to assign data members to an object +var foo = { + val1: function() {}, + val2: null, + val3: 13, + val4: new Object() +}; + +// data members are public +foo.val3 = 33; + +// same 33 output on both +console.log(foo.val3); +console.log(foo['val3']); + + +var bar = new Object(); +bar.foo = function() {}; +bar.foo.val1 = 99; +bar.foo = { + val2: 3.14, + val3: 121 +}; + +console.log(bar.foo['val2']); \ No newline at end of file diff --git a/linkedlist.js b/linkedlist.js new file mode 100644 index 0000000..acae6d9 --- /dev/null +++ b/linkedlist.js @@ -0,0 +1,57 @@ +// linkedlist + +// hides scope from global access +(function() { + function LinkedList() {} + + LinkedList.prototype = { + length: 0, + head: null, + tail: null + }; + + LinkedList.Node = function() {} + LinkedList.Node.prototype = new LinkedList(); + LinkedList.Node.prototype = { + val: null, + prev: null, + next: null + }; + + LinkedList.prototype.add = function(v) { + var newNode = new LinkedList.Node(); + newNode.val = v; + newNode.next = this.head; + if (this.head == null) + this.tail = newNode; + else + this.head.prev = newNode; + this.head = newNode; + this.length++; + + // for mutiple call chaining + return this; + } + + LinkedList.prototype.remove = function() { + } + + LinkedList.prototype.size = function() { + console.log(this.length); + } + + LinkedList.prototype.print = function() { + var curr = this.head; + var res = []; + while (curr) { + res.push(curr.val); + curr = curr.next; + } + console.log(res.join(" -> ")); + } + + var ll = new LinkedList(); + ll.add(7).add(12).add(13).add(21); + ll.print(); + ll.size(); +})(); diff --git a/prototype-this-new.js b/prototype-this-new.js new file mode 100644 index 0000000..96033f4 --- /dev/null +++ b/prototype-this-new.js @@ -0,0 +1,24 @@ +// these are the same, former invokes constructor function +new function() { + console.log('foo'); +}; +(function(){ + console.log('bar'); +})(); +// both functions are unnamed, so there's our only chance to invoke them +// 'new' implies blank object context, e.g. to make instances + +function polygon() { + this.sides = 4; + this.side_length = 4; +} + +var polygonp = function() {}; +polygonp.prototype.perimeter = function() { + polygonp.sides * length_side; +} + +// with new always include (), even when there are no args +var square1 = new polygon(); +var square2 = polygon; +var square3 = polygon(); -- cgit v1.2.3