Javascript Hiring Guide Flashcards

1
Q

Describe Javascript.

A

Javascript is a prototype-based scripting language with dynamic typing.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Describe inheritance and the prototype chain in JavaScript. Give an example.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Compare and contrast objects and hashtables in JavaScript.

A

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]”.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

List ways to create objects.

A
  • var o = Object.create(null);
  • var o = { id: 42, name: ‘John’ };
  • var o = [‘John’];
  • function Entity(id, name) { this.id = id; this.name = name; };
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Difference between function expression and function statement.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the significance of, and reason for, wrapping the entire content of a JavaScript source file in a function block?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the difference between == and ===? Between != and !==? Give an example.

A

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).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the significance of including ‘use strict’ at the beginning of a JavaScript source file?

A

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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Define Hoisting.

A

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!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly