Wednesday, May 28, 2014

JavaScript's Difficult Concepts - Notes

My Notes From: JavaScript's Difficult Concepthttps://joind.in/10647
This is all Vanilla JavaScript.
Nothing about JQuery or MooTools or whatever.

*Scope

Global vs Function
Always use **var** keyword when you name a variable inside a function.
Declare all variables at the top of your function.

*Context

The **this** keyword
callbacks are commonly used in JS???

The __this__ keyword and the context within the function depend entirely on how the function was invokes.

The default context will be the global context for functions invoked by name or variable name within the global context.

.call() is how you use __this__ properly. --> Switch the context of this.
.apply() is different.. allows more params??
Yeah!

Using .call and .apply gives you the control you might need over the context inside a function or method

The **ProtoType Chain**
"The most complicated inheritance model ever created"
Classless, Instance Based

// ** Use a Capital Letter for the function name as a Construction
function Person(name) {
    // blah
}
Person.prototype.talk = function() {
    return "wah";
}

Always use Function.prototype to add methods to your constructor functions for performance reasons.

// Extend the Person object
function Dev(name) {
    Person.call(this, name);
}
Developer.prototype = Object.create(Person.prototype); // Inherit
Developer.prototype.constructor = Developer; // Override constructor type

// Override
Developer.prototype.talk = function() {
    return "wooo";
}

All Objects have the prototype through which we can accompolish inheritcan in JS

Recommendations!!

No comments:

Post a Comment