Javascript Hiring Guide Flashcards
Describe Javascript.
Javascript is a prototype-based scripting language with dynamic typing.
Describe inheritance and the prototype chain in JavaScript. Give an example.
Although JavaScript is an object-oriented language, it is prototype-based and does not implement a traditional class-based inheritance system.
In JavaScript, each object internally references another object, called its prototype.
At the end of this prototype chain is an object with null as its prototype.
The prototype chain is the mechanism by which inheritance is ahcieved in Javascript.
Compare and contrast objects and hashtables in JavaScript.
This is somewhat of a trick question since, in JavaScript, objects essentially are hashtables; i.e., collections of name-value pairs. Keys though are always strings!
var map = new Object();map[foo] = "foo"map[bar] = "bar";
Result will be “bar” for map. Javascript silently converts it to a string and uses that value as the key instead. In this case, “[object Objecct]”.
Explain closures in JavaScript. What are they? What are some of their unique features? How and why might you want to use them? Provide an example.
A closure is a function, along with all variables or functions that were in-scope at the time that the closure was created. In JavaScript, a closure is implemented as an “inner function”; i.e., a function defined within the body of another function.
An important feature of closures is that an inner function still has access to the outer function’s variables even after the outer function has returned. This is because, when functions in JavaScript execute, they use the scope that was in effect when they were created.
List ways to create objects.
- var o = Object.create(null);
- var o = { id: 42, name: ‘John’ };
- var o = [‘John’];
- function Entity(id, name) { this.id = id; this.name = name; };
Difference between function expression and function statement.
There is a difference based on how and when the value of the function is assigned.
A function statement ( function foo() {} ) may be referenced before it has been defined, known as hoisting. The ramification though is that if it is defined twice, the last function will be declared as the true function.
A function expression may not be referenced before it is defined.
What is the significance of, and reason for, wrapping the entire content of a JavaScript source file in a function block?
This technique creates a closure around the entire contents of the file which, perhaps most importantly, creates a private namespace thereby helps avoid potential name clashes between difference Javascript modules and libraries.
Another feature of this technique is to allow for an easily referenceable (presumably shorter) alias for a global variable. jQuery plugins utilize this.
What is the difference between == and ===? Between != and !==? Give an example.
In JavaScript the double comparison operators perform implicit type conversion on the operands before comparing them whereas, with the triple comparison operators, no type conversion is performed (i.e., the values must be equal and the types must be the same for the operands to be considered equal).
What is the significance of including ‘use strict’ at the beginning of a JavaScript source file?
The short and most important answer here is that use strict is a way to voluntarily enforce stricter parsing and error handling on your JavaScript code at runtime. Code errors that would otherwise have been ignored or would have failed silently will now generate errors or throw exceptions.
- Makes debugging easier.
- Prevents accidental globals.
- Eliminates ‘this’ coercion. Without strict mode, ‘this’ if null or undefined will coerce to the global window object.
- Disallows duplicate property names or parameter values.
- Makes eval() safer.
- Throws error on invalid usage of delete.
Define Hoisting.
When compiling Javascript, function declarations are hoisted to the top of their scope. It can even be declared after return value and still be fine!