Javascript - The Good Parts - General Flashcards
(39 cards)
Describe the “method” helper method used in the book
Function.prototype.method = function (name, func) {
this.prototype[name] = func;
return this;
};
Describe Javascript’s prototype linkage feature
allows one object to inherit the properties of another. When used well, this can reduce object initialization time and memory consumption.
What is an object literal?
An object literal is a pair of curly braces surrounding zero or more name/value pairs.
Javascript objects are passed by …
reference.
What’s the difference between pass by reference and pass by value?
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.
Prototype description:
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
Describe prototype delegation.
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.
Describe the prototype relationship being dynamic.
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:
What does the hasOwnProperty method perform?
returns true if the object has a particular property. The hasOwnProperty method does not look at the prototype chain:
Describe the delete operator
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:
Give an overview of the Function object
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.
What is a function literal?
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.
Give an overview of the function invocation process.
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.
What are the four invocation patterns?
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.
What is the Method invocation pattern?
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.
What is the Function invocation pattern?
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:
What does this mean: JavaScript is a prototypal inheritance language.
That means that objects can inherit properties directly from other objects. The language is class-free.
What is the Constructor invocation pattern?
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.
What is the Apply invocation pattern?
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
What is the arguments array?
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.
Describe a Javascript throw.
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.
Describe augmenting types in Javascript
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.
What is a defensive technique when augmenting types?
add a method only if the method is known to be missing:.
Function.prototype.method = function (name, func) {
if (!this.prototype[name]) { … }
}
What is scope?
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