JavaScript: The Good Parts: The Good Parts
Rate it:
Open Preview
Kindle Notes & Highlights
4%
Flag icon
Programming is difficult business. It should never be undertaken in ignorance.
4%
Flag icon
JavaScript’s functions are first class objects with (mostly) lexical scoping.
4%
Flag icon
The fashion in most programming languages today demands strong typing. The theory is that strong typing allows a compiler to detect a large class of errors at compile time. The sooner we can detect and repair errors, the less they cost us. JavaScript is a loosely typed language, so JavaScript compilers are unable to detect type errors. This can be alarming to people who are coming to JavaScript from strongly typed languages. But it turns out that strong typing does not eliminate the need for careful testing.
5%
Flag icon
JSLint provides a degree of rigor that is generally lacking in JavaScript development. It can give you confidence that your programs contain only the good parts.
5%
Flag icon
functional programming is a lot of fun.
5%
Flag icon
The good parts are good enough to compensate for the bad parts.
6%
Flag icon
So, it is recommended that /* */ comments be avoided and // comments be used instead. In this book, // will be used exclusively.
6%
Flag icon
The list does not include some words that should have been reserved but were not, such as undefined, NaN, and Infinity.
6%
Flag icon
JavaScript has a single number type. Internally, it is represented as 64-bit floating point, the same as Java’s double.
7%
Flag icon
A string literal can be wrapped in single quotes or double quotes. It can contain zero or more characters. The \ (backslash) is the escape character. JavaScript was built at a time when Unicode was a 16-bit character set, so all characters in JavaScript are 16 bits wide.
7%
Flag icon
A compilation unit contains a set of executable statements. In web browsers, each <script> tag delivers a compilation unit that is compiled and immediately executed. Lacking a linker, JavaScript throws them all together in a common global namespace.
Clara
?
8%
Flag icon
A block is a set of statements wrapped in curly braces. Unlike many other languages, blocks in JavaScript do not create a new scope, so variables should be defined at the top of the function, not in blocks.
9%
Flag icon
for in)
9%
Flag icon
It is usually necessary to test object.hasOwnProperty(variable) to determine whether the property name is truly a member of the object or was found instead on the prototype chain. for (myvar in obj) {     if (obj.hasOwnProperty(myvar)) {         ...     } }
9%
Flag icon
The += operator can add or concatenate.
10%
Flag icon
infix operator
Clara
?
10%
Flag icon
Unary operators
Clara
?
10%
Flag icon
Ternary
Clara
?
12%
Flag icon
The simple types of JavaScript are numbers, strings, booleans (true and false), null, and undefined. All other values are objects.
12%
Flag icon
Objects in JavaScript are mutable keyed collections.
13%
Flag icon
The || operator can be used to fill in default values: var middle = stooge["middle-name"] || "(none)"; var status = flight.status || "unknown";
Clara
?
13%
Flag icon
Objects are passed around by reference. They are never copied:
13%
Flag icon
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.
14%
Flag icon
The prototype relationship is a dynamic relationship. 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:
14%
Flag icon
There are two approaches to dealing with these undesired properties. The first is to have your program look for and reject function values. Generally, when you are reflecting, you are interested in data, and so you should be aware that some values could be functions.
14%
Flag icon
The other approach is to use the hasOwnProperty method, which returns true if the object has a particular property. The hasOwnProperty method does not look at the prototype chain:
14%
Flag icon
The most common filters are the hasOwnProperty method and using typeof to exclude functions: var name; for (name in another_stooge) {     if (typeof another_stooge[name] !== 'function') {         document.writeln(name + ': ' + another_stooge[name]);     } }
14%
Flag icon
There is no guarantee on the order of the names, so be prepared for the names to appear in any order. If you want to assure that the properties appear in a particular order, it is best to avoid the for in statement entirely and instead make an array containing the names of the properties in the correct order:
15%
Flag icon
Removing a property from an object may allow a property from the prototype linkage to shine through: another_stooge.nickname    // 'Moe' // Remove nickname from another_stooge, revealing // the nickname of the prototype. delete another_stooge.nickname; another_stooge.nickname    // 'Curly'
15%
Flag icon
By reducing your global footprint to a single name, you significantly reduce the chance of bad interactions with other applications, widgets, or libraries. Your program also becomes easier to read because it is obvious that MYAPP.stooge refers to a top-level structure. In the next chapter, we will see ways to use closure for information hiding, which is another effective global abatement technique.
16%
Flag icon
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.
58%
Flag icon
Use of closure provides further information hiding, increasing the strength of my modules.