Study JavaScript for Interviews

I have an upcoming job interview and have been doing my JavaScript research and review. Here is a great list of JavaScript resources that I’ve been using:

Videos
JavaScript Closure: http://www.youtube.com/watch?v=oWSQ4mWNFPU
JavaScript Hoisting: http://www.youtube.com/watch?v=sw49K4pxHCU

Reading
Mozilla Developer: Re-Intro to JS
https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript

Objects
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript

Activities
CodeAcademy – Free
http://www.codecademy.com/tracks/javascript

CodeSchool – Free / $25-month:
Some courses are free, the rest require membership $25 / month, but if you’re job hunting get it for a month!)
jQuery, JS, & CSS and HTML: https://www.codeschool.com/courses/try-jquery

References
W3 Schools JS: http://www.w3schools.com/js/js_objects.asp


 Quick Notes

-Everything is an object
-JavaScript does not have Classes, but it does have prototypes

 

Object literal

var person={name:"David",age:24};

Direct Instance (pretty much same as object literal)

var person = new Object(); // {}; is the preferred syntax.
person.name="david";
person.age=24;
person.getName = function () {
  return this.name;
};

Constructor with private, privileged and public methods

function Person(fName, lName, age){
  // Public properties
  this.firstname = fName;
  this.lastname = lName;
  this.age=age;

  // Private properties: use var for private.
  // accessible to local private methods and to privileged functions
  var privateName = fName+"secrets";

  // Private method
  function privateFunc(){
    // This function can access private vars and methods.
    // Can only be called from within Person.
  }

  //Privileged method
  this.privilegedFunc = function(){
    // This function can access private vars and methods.
    // Can be called from outside or inside Person
  }
}

Person.prototype.publicFunc = function(){
  //this public function can only access public properties of Person
  return this.age;
};

Scoping: private, public, priviliged

Private and privileged members can only be made when an object is constructed.

Private: must be inside the constructor. Use var for private properties, and for private methods use: function funcName(){…};

Privileged: method is able to access the private variables and methods, and is itself accessible to the public methods and the outside. It is possible to delete or replace a privileged method, but it is not possible to alter it, or to force it to give up its secrets.

Public: For best memory usage add public functions to the object’s prototype

Person.prototype.getName(){…};

More: http://www.crockford.com/javascript/private.html


Three ways to call Methods

//V1
fatRabbit.speak("Burp");

//V2: call first param is the context, can be null, then pass the method's params
speak.call(fatRabbit,"Burp");

//V2.5: apply same as call, but method's params are passed as an array
Speak.apply(fatRabbit,["Burp"]);

 


Inheritance
-point child to parent prototype

// define the Student class
function Student() {
  // Call the parent constructor
  Person.call(this);
}
// inherit Person
Student.prototype = new Person();

// correct the constructor pointer because it points to Person
Student.prototype.constructor = Student;

Interfaces (don’t exist)
There’s no notion of “this class must have these functions” (that is, no interfaces per se), because:
1. JavaScript inheritance is based on objects, not classes. That’s not a big deal until you realize:
2. JavaScript is an extremely dynamically typed language — you can create an object with the proper methods, which would make it conform to the interface, and then undefine all the stuff that made it conform. It’d be so easy to subvert the type system — even accidentally! — that it wouldn’t be worth it to try and make a type system in the first place.

Interface-ish
-if you need to make sure an object has a certain function, do it like this:

 
if (typeof(someObject.quack) == "function")
{
  // This thing can quack
}

http://stackoverflow.com/questions/3710275/does-javascript-have-the-interface-type-such-as-javas-interface


Static
To create a static property attach it to the ‘class’, not an instance.(class in quotes cause in Js they’re all objects.)
Example with a Person ‘class’:

function Person(){
  //…
}
Person.staticProperty = 2;

http://stackoverflow.com/questions/1535631/static-variables-in-javascript


Const
Nothing’s ever constant, but you can use conventions like ALL_CAPS to show that certain values should not be modified.


TidBits

Closure
Function’s parents can return and get garbage collected, but if the child function still exists it will still be able to see all of the variables it had access to when the parent was alive.

Hoisting
Variable declarations (not initializations) are hoisted to the top of their scope.
If it’s a var = function(){…} then the whole function is hoisted up.

These 2 functions are the same:

function membername(...) {...}
var membername = function membername(...) {...};

Sort an array of ints

ary.sort(function(a,b){return a-b }); //ary will be modified by sort.

Number and parseInt

//parseInt accepts base as second optional param
// parsing:
parseInt("20px"); // 20
parseInt("10100", 2); // 20
parseInt("2e1"); // 2

// type conversion
Number("20px"); // NaN
Number("2e1"); // 20, exponential notation

Number("010"); // 10
parseInt("010"); // 8, implicit octal
parseInt("010", 10); // 10, decimal radix used
But it can handle numbers in hexadecimal notation, just like parseInt:

Number("0xF"); // 15
parseInt("0xF"); //15