More JavaScript Stuff
As I’m navigating the job market now it’s becoming more and more apparent that I need to get up to date with JavaScript, especially if I want to do anything on the front-end. I’m starting to work on some projects that make more use of it ( see: 5aurapod ) and just dig in deeper with tutorials and questions. Some of this is based on questions I’ve been asked in interviews and some is based on just exploring:
Data Types
There are 5 primitive data types in JavaScript: Booleans, Numbers, Strings, Null, and Undefined. Booleans, Strings, and Numbers are the primary data types and Null and Undefined are special types.
You can check the type of a variable by using the typeof() function.
Some Scope Issues
An interesting question I found is as follows:
What is the difference between this:
function foo() { var i = 3;
console.log(i);
}
and this…
function foo() { i = 3;
console.log(i);
}
The difference is that in the first one, a variable i will be declared in the local scope of foo() and set to 3. The second one searches for i at all available scopes, including global and sets it to 3.
So with the first function if I try to run:
foo();
console.log(i);
I’ll get an output of
3
i is not defined
Where with function 2 I get:
3
3
Closures
( Taken from: http://robertnyman.com/2008/10/09/explaining-javascript-scope-and-closures/ This is a great resource and I should refer back to it often!)
Closures are expressions, usually functions, which can work with variables set within a certain scope. Inner functions referring to local variables of its outer functions create closures. This is the example given:
function add (x) {
return function (y) {
return x + y; };
}
var add5 = add(5);
var no8 = add5(3);
alert(no8); // Returns 8
Ok, so what’s happening here is: the function add(x) returns a function that will add x to whatever is passed into that function. no8() is created by taking in add5(3) which adds the value of y (3 in this case) to 5 as previously defined when add5 was created.
Prototypes
I NEED TO LEARN MORE ABOUT PROTOTYPES. CHECK OUT THESE THINGS: http://yehudakatz.com/2011/08/12/understanding-prototypes-in-javascript/ AND http://net.tutsplus.com/tutorials/javascript-ajax/prototypes-in-javascript-what-you-need-to-know/
Leave a comment
Calendar
| S | M | T | W | T | F | S |
|---|---|---|---|---|---|---|
| « Sep | ||||||
| 1 | 2 | 3 | 4 | |||
| 5 | 6 | 7 | 8 | 9 | 10 | 11 |
| 12 | 13 | 14 | 15 | 16 | 17 | 18 |
| 19 | 20 | 21 | 22 | 23 | 24 | 25 |
| 26 | 27 | 28 | 29 | 30 | 31 | |
Recent Posts
Categories
- Academics (11)
- Beer Brewing (15)
- EP (6)
- Guitar (53)
- Hacking (17)
- Mandarin (1)
- Meta (10)
- Piano (18)
- Vocals (42)
- Web Development (12)
Tags
Archives
- September 2012 (1)
- August 2012 (5)
- July 2012 (6)
- June 2012 (12)
- May 2012 (19)
- April 2012 (22)
- March 2012 (25)
- February 2012 (49)
- January 2012 (46)


