На прошлых выходных прочитал книгу JavaScript: The Good Parts. На этих решил опубликовать несколько интересных и забавных на мой взгляд цитат.
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.
Since functions are objects, they can be used like any other value. Functions can be stored in variables, objects, and arrays. Functions can be passed as arguments to functions, and functions can be returned from functions. Also, since functions are objects, functions can have methods. The thing that is special about functions is that they can be invoked.
The best thing about JavaScript is its implementation of functions. It got almost everything right. But, as you should expect with JavaScript, it didn’t get everything right.
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:
var that = this; // Workaround.
The toLocaleUpperCase method produces a new string that is made by converting this string to uppercase using the rules for the locale. This is primarily for the benefit of Turkish, because in that language ‘i’ converts to ‘İ’, not ‘I’.
I prefer to make the structure of my programs self-illuminating, eliminating the need for comments. I am not always successful, so while my programs are awaiting perfection, I am writing comments.
The following words are reserved in JavaScript:
abstract boolean break byte case catch char class const continue debugger default delete do double else enum export extends false final finally float for function goto if implements import in instanceof int interface long native new null package private protected public return short static super switch synchronized this throw throws transient true try typeof var volatile void while withMost of these words are not used in the language.
The statement:
with (obj) {a = b;}does the same thing as:
if (obj.a === undefined) {a = obj.b === undefined ? b : obj.b;} else {obj.a = obj.b === undefined ? b : obj.b;}
In many languages, void is a type that has no values. In JavaScript, void is an operator that takes an operand and returns undefined. This is not useful, and it is very confusing. Avoid void.
No Comments Yet