Tuesday, November 4, 2014

JavaScript notes - part 1

Hello. In this post I will share some notes I made while reading this book JavaScript: The Good Parts by Douglas Crockford. So it is nothing new or special, this post will serve simply to cover the most important properties of JavaScript. The notes are taken, copied or modified from the book itself. I didn't write them, Douglas did. :)

For part two visit this link: http://tunephp.blogspot.com/2014/11/javascript-notes-part-2.html
 

Good Parts

  1. I can be a better programmer by using only the good parts of a language and avoiding the bad parts.
  2. The API of the browser, Document Object Model (DOM) is quite awful and JavaScript is unfailry blamed.
  3. JavaScript is a language with enromous expressive  power. It is even better when you know what you are doing.
  4. Very good ideas on which JS is build upon are: functions, loose typing, dynamic objects, and an expressive object literal notation. Bad ideas include global variables.
  5. JavaScript has more in common with Lisp and Scheme than with Java. It is List in C - clothing.
  6. JS compilers are unable to detect type errors because JS is loosely typed.
  7. Objects can be created by listing their components (inspired by object literal notation mainly for the creation of the JSON format).
  8. JS has class free object system in which objects inherit properties directly from other objects (prototypal inheritance).
  9. Implementing classical design  patterns in JS can be frustrating but using prototypal inheritance can be rewarding.
  10. JS depends on global variables for linkage. All top level vars are tossed together in a common namespace called global object.

Grammar

  1. JS supports two types of comments, block comments made by /* .. */ charaters and line - ending comment made by two forward slashes // . Comments should be updated and should always describe accurately what the code does. Obsolete comments are worse than no comments.
  2.  Block comments should be avoided as it is unsafe to comment out a block of code. Use line ending comments instead.
  3. The list of reserved words should include theese: undefined, NaN and Infinity but unfortunately it doesn't have them. It is not permited to use reserved word as a name of a variable. Be careful with the three mentioned above.
  4. JS has a single number type. Internally it is represented as a 64 bit floating point number, same as Java's double number type.  There is no separate number type, 1 and 1.0 are the same thing.
  5. 1e2 = 100 = 1 * 10 ^ 2
  6. NaN is a number value produced as result of operation that cannot produce normal result. NaN is not equal to any value including itself. You can detect a NaN value with isNaN(value) function, or you can use isFinite(value) function which returns false if the value is +Infinity, -Infinity, or NaN (Not-a-Number), otherwise it returns true.
  7. Infinity represents all values greater than 1.7976931348623157E+10308
  8. Numbers have methods. JS has Math object that contain set of methods to work on numbers.
  9. A string literal can be wrapped  in single or in double quote. (\) backslash is the escape character. 
  10. All characters in JS are of 16 bit character set (JS was built when Unicode was 16 bit). There is not character type. Today Unicode has more than 65536 different characters represented by 32 bits. Be aware that JavaScript decodes them as two characters instead of single one.
  11. The \u convention allows specifying characters numerically. "A" === "\u0041";
  12. Strings have length property. A string is immutable constant. Once set a new value you cannot change it. However new strings can be made by concatenating two separate strings with + sign.
  13. Unlike many other languages blocks of code surrounded by curly braces {} do not create a new scope, so new variables should be defined at the top of the function, not in blocks.
  14. Falsy values in JS are: false, null, undefined, the empty string '', the number 0, the number NaN. All other values including true, the string 'false' and all objects are truthy.
  15. Use object.hasOwnProperty(variable) function to determine whether the property name is truly a member of the object or was found instead on the prototype chain.
  16. The = operator is used for assignment. Do not confuse it with equality operator ===.

Objects

  1. The simple types of JS include numbers, strings, booleans, null and undefined. All other values are objects.
  2. Numbers, strings and booleans are object like and they have methods but they are immutable.
  3. Objects in JS are mutable nested collections of properties. In JavaScript arrays are objects, functions are objects, regular expressions are objects and of course objects are objects.
  4. Objects in JS are class free. Objects can contain objects.
  5. JS includes prototype linkage, which allows object to inherit properties from another objects. When properly used prototype linkage can reduce object initialization time and memory consumption.
  6. The quotes around a property's name in an object literal are optional if the name would be legal and not a reserved word. Quotes around "first-name" are required but optional around first-name.
  7. If a string expression is a constant and a legal JS name and not a reserved word than the dot ( . ) notation can be used instead.
  8. Attempting to retrieve undefined values from object will throw a TypeError exception.
  9. If there is already a property with given name in an object, new value will just replace the old value. If the propery does't exist the object is augmented with new property. 
  10. Objects are passed around by reference, they are never copied.
  11. Every object is linked to a prototype object from which it can inherit properties. All objects created from object literal are linked to Object.prototype.
  12. The hasOwnProperty() method does not look at the prototype chain of an object.
  13. The "for in" statement can loop over all of the property names in an object. It will include functions and prototype properties as well. The order of the properties/functions listed using "for in" is not necessarily incremental. If the order is important use "for" statement instead. "for" does not cover the prototype properties.
  14. delete operator can be used to remove a property from an object. It will not touch any of the objects in the prototype linkage. So removing a property from object may uncover a property from the prototype object with the same name.
  15. Avoid using global variables. Better create your own application namespace by creating your own global variable in which all your application structure will reside so that it will not interfere with objects/properties from other applications in the global namespace. 

Functions

  1. Functions are the best part in JavaScript. They are commonly used for code reuse, information hiding and composition. Functions in JS are objects linked with Function.prototype which is linked to Object.prototype
  2. Functions can be stored in variables, objects, arrays. Functions can be passed as arguments to function and functions can be returned from functions. Also since functions are objects they can have methods too. Unlike other objects in JS, functions can be invoked.
  3. Function literal has 4 parts. The first is the word "function". The second optional is the name of a function (used by debbugers to identify a function). Function without a name is called anonymous function. The third part is a set of parameters wrapped in braces (). The fourth part is a set of statements wrapped in curly braces {} (The body of a function).
  4. In addtion to the list of parameters in braces (), each function gets two additional params: this and arguments
  5. There are four patterns of invoaction in JS: method invocation, function invocation, constructor invocation and apply invocation (the patterns differ in how the bonus parameter "this" is initialized).
  6. There is no runtime error when the number of arguments and the number of parameters do not match. If there are too many argument values, the extra args are ignored. If there are too few arguments, the missing ones will be substituted with undefined. There is no type checking on the argument values, any type of value can be passed to any parameter.
  7. When a function is invoked with function invocation pattern, it is bound to the global object. If the language would have been designed correctly, the function would be bound to "this" of the outer function. 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.
  8. 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.
  9. Functions that are intended to be used with the new prefix are called contrustors. By convention they are kept in variables with a capitalized name. If a contructor is called without the new prefix very bad things can happen without a compile time or runtime warning, so the capitalization convention is really important. The best way to cope with this is to not use new prefix at all.
  10. arugments array gives the function all the arguments that were supplied with the invocation, including excess arguments that were not assigned to parameters.
  11. Because of design error, arguments is not really an array. It is an array-like object. It has the length property but it lacks array methods.
  12. A function always returns a value. If the return value is not specified then undefined is returned.
  13. If the function was invoked with the new prefix and the return value is not an object then "this" (the new object) is returned instead.
  14. 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.
  15. Unfotunatelly JS does not currently provide tail recursion optimization.
  16. JavaScript doesn't have a block scope but it has function scope. That means all variables should be declared as soon as possible or at the top of the function body.
  17. The good thing about scope is that inner functions get access to the parameters and variables of the functions they are defined within (exception are "this" and arguments object). This is called Closure.
  18. A module is a function or object that represents an interface but that hides its state and implementation.
  19. By using functions to produce modules, we can completely eliminate our use of global variables thereby mitigating one of the worst JS features.
  20. Use of module pattern can eliminate the use of global variables.
  21. JavaScript does not have a Curry method.
  22. Function can use objects to remember the results of previous operations making it possible to avoid unnecessary work. This optimization is called memoization.
  23. By devising functions that produce other functions, we can significantly reduce the amout of work we have to do. 

No comments:

Post a Comment