Javascript - The Good Parts - General Flashcards

(39 cards)

1
Q

Describe the “method” helper method used in the book

A

Function.prototype.method = function (name, func) {
this.prototype[name] = func;
return this;
};

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

Describe Javascript’s prototype linkage feature

A

allows one object to inherit the properties of another. When used well, this can reduce object initialization time and memory consumption.

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

What is an object literal?

A

An object literal is a pair of curly braces surrounding zero or more name/value pairs.

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

Javascript objects are passed by …

A

reference.

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

What’s the difference between pass by reference and pass by value?

A

Think of URL. Pass by reference - we share same url that access a web page. WHen it’s updated, we both see theu pdate. Pass by value would be printing off the web page and giving your friend the page. Your page is a disconnected copy of the original.

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

Prototype description:

A

Every object is linked to a prototype object from which it can inherit properties. All objects created from object literals are linked to Object.prototype, an object that comes standard with JavaScript. When you make a new object, you can select the object that should be its prototype

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

Describe prototype delegation.

A

The prototype link is used only in retrieval. If we try to retrieve a property value from an object, and if the object lacks the property name, then JavaScript attempts to retrieve the property value from the prototype object. And if that object is lacking the property, then it goes to its prototype, and so on until the process finally bottoms out with Object.prototype. If the desired property exists nowhere in the prototype chain, then the result is the undefined value. This is called delegation.

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

Describe the prototype relationship being dynamic.

A

If we add a new property to a prototype, that property will immediately be visible in all of the objects that are based on that prototype:

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

What does the hasOwnProperty method perform?

A

returns true if the object has a particular property. The hasOwnProperty method does not look at the prototype chain:

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

Describe the delete operator

A

The delete operator can be used to remove a property from an object. Removing a property from an object may allow a property from the prototype linkage to shine through:

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

Give an overview of the Function object

A

Functions in JavaScript are objects. Objects are collections of name/value pairs having a hidden link to a prototype object. Objects produced from object literals are linked to Object.prototype. Function objects are linked to Function.prototype (which is itself linked to Object.prototype). Every function is also created with two additional hidden properties: the function’s context and the code that implements the function’s behavior.

Every function object is also created with a prototype property. Its value is an object with a constructor property whose value is the function. This is distinct from the hidden link to Function.prototype.

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

What is a function literal?

A

Function objects are created with function literals. The function object created by a function literal contains a link to that outer context. This is called closure. This is the source of enormous expressive power.

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

Give an overview of the function invocation process.

A

Invoking a function suspends the execution of the current function, passing control and parameters to the new function. In addition to the declared parameters, every function receives two additional parameters: this and arguments. The this parameter is very important in object oriented programming, and its value is determined by the invocation pattern.

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

What are the four invocation patterns?

A

the method invocation pattern, the function invocation pattern, the constructor invocation pattern, and the apply invocation pattern. The patterns differ in how the bonus parameter this is initialized.

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

What is the Method invocation pattern?

A

When a function is stored as a property of an object, we call it a method. When a method is invoked, this is bound to that object. Methods that get their object context from this are called public methods.

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

What is the Function invocation pattern?

A

When a function is not the property of an object, then it is invoked as a function. When a function is invoked with this pattern, this is bound to the global object. A consequence of this error is that a method cannot employ an inner function to help it do its work because the inner function does not share the method’s access to the object as its this is bound to the wrong value. Fortunately, there is an easy workaround. If the method defines a variable and assigns it the value of this, the inner function will have access to this through that variable. By convention, the name of that variable is that:

17
Q

What does this mean: JavaScript is a prototypal inheritance language.

A

That means that objects can inherit properties directly from other objects. The language is class-free.

18
Q

What is the Constructor invocation pattern?

A

If a function is invoked with the new prefix, then a new object will be created with a hidden link to the value of the function’s prototype member, and this will be bound to that new object. Use of this style of constructor functions is not recommended.

19
Q

What is the Apply invocation pattern?

A

The apply method lets us construct an array of arguments to use to invoke a function. It also lets us choose the value of this. The apply method takes two parameters. The first is the value that should be bound to this. The second is an array of parameters

20
Q

What is the arguments array?

A

It gives the function access to all of the arguments that were supplied with the invocation, including excess arguments that were not assigned to parameters.

21
Q

Describe a Javascript throw.

A

The throw statement interrupts execution of the function. It should be given an exception object containing a name property that identifies the type of the exception, and a descriptive message property. The exception object will be delivered to the catch clause of a try statement. A try statement has a single catch block that will catch all exceptions. If your handling depends on the type of the exception, then the exception handler will have to inspect the name to determine the type of the exception.

22
Q

Describe augmenting types in Javascript

A

adding a method to Object.prototype makes that method available to all objects. This also works for functions, arrays, strings, numbers, regular expressions, and booleans.

23
Q

What is a defensive technique when augmenting types?

A

add a method only if the method is known to be missing:.
Function.prototype.method = function (name, func) {
if (!this.prototype[name]) { … }
}

24
Q

What is scope?

A

Scope in a programming language controls the visibility and lifetimes of variables and parameters. This is an important service to the programmer because it reduces naming collisions and provides automatic memory management

25
What is function scope?
That means that the parameters and variables defined in a function are not visible outside of the function, and that a variable defined anywhere within a function is visible everywhere within the function.
26
Does Javascript have block scope?
Nope. Block scope being all variables defined in a block (a list of statements wrapped with curly braces) are not visible from outside of the block.
27
Describe the concept of closure.
A function has access to the context in which it was created.
28
Give an example of creating private variables utilizing closure.
Instead of initializing myObject with an object literal, we will initialize myObject by calling a function that returns an object literal. That function defines a value variable. That variable is always available to the increment and getValue methods, but the function's scope keeps it hidden from the rest of the program ``` var myObject = (function () { var value = 0; ``` ``` return { increment: function (inc) { value += typeof inc === 'number' ? inc : 1; }, getValue: function ( ) { return value; } }; }()); ```
29
What is a module in Javascript?
We can use functions and closure to make modules. A module is a function or object that presents an interface but that hides its state and implementation.
30
What is cascade and how do we enable this?
In a cascade, we can call many methods on the same object in sequence in a single statement. Some methods do not have a return value. For example, it is typical for methods that set or change the state of an object to return nothing. If we have those methods return this instead of undefined, we can enable cascades.
31
What is currying?
``` Currying allows us to produce a new function by combining a function and an argument. var add1 = add.curry(1); document.writeln(add1(6)); // 7 ``` WHERE ``` Function.method('curry', function ( ) { var slice = Array.prototype.slice, args = slice.apply(arguments), that = this; return function ( ) { return that.apply(null, args.concat(slice.apply(arguments))); }; }); ```
32
What is memoization?
Functions can use objects to remember the results of previous operations, making it possible to avoid unnecessary work. This optimization is called memoization. JavaScript's objects and arrays are very convenient for this. Think of fibonacci recursive function example.
33
What is an Object Specifier?
An object argument instead of having too many parameters. The object can be accessed within the internal function.
34
Give an overview of functional inheritance.
We start by making a function that will produce objects. We will give it a name that starts with a lowercase letter because it will not require the use of the new prefix. The function contains four steps: It creates a new object. There are lots of ways to make an object. It can make an object literal, or it can call a constructor function with the new prefix, or it can use the Object.create method to make a new instance from an existing object, or it can call any function that returns an object. It optionally defines private instance variables and methods. These are just ordinary vars of the function. It augments that new object with methods. Those methods will have privileged access to the parameters and the vars defined in the second step. It returns that new object.
35
Give an example of functional inheritance in code.
constructor = function (spec, my) { var that, other private instance variables; my = my || {}; Add shared variables and functions to my that = a new object; Add privileged methods to that return that; };
36
What is the typeof operator of an array?
object
37
What are the "good parts" of Javascript?
``` 1) Functions as first class objects Functions in Simplified JavaScript are lambdas with lexical scoping. ``` 2) Dynamic objects with prototypal inheritance Objects are class-free. We can add a new member to any object by ordinary assignment. An object can inherit members from another object. 3) Object literals and array literals This is a very convenient notation for creating new objects and arrays. JavaScript literals were the inspiration for the JSON data interchange format.
38
What's the difference between == and ===
JavaScript has two sets of equality operators: === and !==, and their evil twins == and !=. The good ones work the way you would expect. If the two operands are of the same type and have the same value, then === produces true and !== produces false. The evil twins do the right thing when the operands are of the same type, but if they are of different types, they attempt to coerce the values. The rules by which they do that are complicated and unmemorable.
39
What is functional hoisting?
Function statements are subject to hoisting. This means that regardless of where a function is placed, it is moved to the top of the scope in which it is defined. This relaxes the requirement that functions should be declared before used, which I think leads to sloppiness.