May 21, 2012
Dave

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

Input Coffee is my blog for writing about all the projects I'm working on. I think that it helps me to write about what I'm working on. I am mainly writing these posts for my own benefit, but if anybody else finds value in them or has any helpful comments, I certainly welcome that.

Calendar

May 2013
S M T W T F S
« Sep    
 1234
567891011
12131415161718
19202122232425
262728293031  

Categories

Archives