Comments:"JavaScript: The Important Parts | Ben Lakey"
URL:http://benlakey.com/2013/05/26/javascript-the-important-parts/
I came to the JavaScript party much later than some of my colleagues but much earlier than most. I was told the best resource was Douglas Crockford’s book ‘JavaScript: The Good Parts‘ and was not disappointed. The book is thin and contains a minimal set of information needed to get you in a good state with JavaScript. While reading through it however I still found much of the book to be information I was already aware of, either with JavaScript syntax or basic programming concepts, and so I extracted those bits of knowledge that were most impactful against my existing knowledge level going in. Here is that list.
DOM
The DOM is not a JavaScript concept. It is an object model used by web browsers and is inconsistently implemented across the various browsers.
Functions and ‘this’
- When functions are invoked as methods on an object (‘.’ refinement), ‘this’ is bound to the object.
- When functions are invoked without ‘.’ refinement, ‘this’ is bound to the global object, regardless of what scope this happens in.
- When functions are invoked with ‘new’ keyword prefix: A new object is created. ‘this’ is bound to that new object. The function is executed.
- When functions are invoked with ‘apply’, you can choose ‘this’ and the parameters. Example: fooFunction.apply(context, stuff);
Inheritance
For inheritance, Javascript is prototypal, not class-based. When method calls occur, the current object is inspected for the method. If the method isn’t found, we walk up the prototype chain until it is found, or we bottom out at the root object. We can apply a method to a all objects that inherit down the chain by adding the method to an object’s prototype. All of this means that it doesn’t matter what an object’s lineage is, only what it can do at the moment.
Inheritance heirarchies can be crafted by doing something like the following:
var Bar = function(name) { this.name; }; Bar.prototype = new Foo(); var barInstance = new Bar(); //barInstance has all the stuff from Bar as well as Foo.
Security
- eval(str) is dangerous because it executes whatever javascript can be passed in by end clients.
- setTimeout() and setInterval() act as eval() for string arguments.