from Bad Parts: Appendix B - JavaScript: The Good Parts
How often do these issues come up?
how many with
bugs have you wrangled?
this
work?this
var obj = {
fullName:"John K. Paul",
alertName:function(greeting){
alert(greeting + " " + this.fullName);
}
};
obj.alertName("Hey"); // Hey John K. Paul
var obj = {
fullName:"John K. Paul",
alertName:function(greeting){
alert(greeting + " " + this.fullName);
}
};
var alertName = obj.alertName;
alertName("Hey"); // Hey undefined
// this is set to the global object unless in strict mode
var obj = {
fullName:"John K. Paul",
alertName:function(greeting){
alert(greeting + " " + this.fullName);
}
};
var alertName = obj.alertName;
var newContext = {
fullName:"John Doe"
};
alertName.call(newContext, "Hey"); // Hey John Doe
var obj = {
fullName:"John K. Paul",
alertName:function(greeting){
alert(greeting + " " + this.fullName);
}
};
var alertName = obj.alertName;
var newContext = {
fullName:"John Doe"
};
alertName.apply(newContext, ["Hey"]); // Hey John Doe
var Constructor = function(){
this.sayHey = function(){
alert("Hey");
}
}
//this is a newly created object
var newObject = new Constructor();
newObject.sayHey() // Hey
In ES6
// with fat arrow inner function
var myObj = {
longOuter: function() {
console.log(this); // this is myObj
var fatInner = () =>
console.log(this); // this is also myObj
fatInner();
}
}
myObj.longOuter();
What is inheritance...really?
Is it classes
?
Is it blueprints
?
Is it super
?
No, none of these
This is what we really care about
Write code once, use in multiple situations
prototypal inheritance makes this easy
don't think about new
, super
, and extends
don't even think about javascript's prototype property
Prototypal inheritance is about fallbacks
var base = {
firstName:"John",
lastName:"Paul",
getFullName:function(){
return this.firstName + " " + this.lastName;
},
}; // base is the fallback
var obj = Object.create(base); // where the magic happens
obj.alertName = function(){
alert(this.getFullName());
};
obj.alertName(); // John Paul
// this is code reuse!
alert("getFullName" in obj) //true
alert(obj.getFullName === base.getFullName) //true
Not all js environments have Object.create
But we have a solution for < IE9
Object.closeToCreate = function (o) {
//as close to Object.create's functionality in old IEs
function F() {}
F.prototype = o;
return new F();
};
prototype
is double speakfallbackOfObjectsCreatedWithNew
would be better
var arr = [1,2,3];
var out = [];
for(var i = 0; i<arr.length;i++) {
var item = arr[i];
out.push(function(){ alert(item); });
}
out.forEach(function(func){ func(); });
var arr = [1,2,3];
var out = [];
for(var i = 0; i<arr.length;i++) {
(function(valueToAlert){
var item = valueToAlert;
out.push(function(){ alert(item); });
})(arr[i]);
}
out.forEach(function(func){ func(); });
do
keywordlet
var arr = [1,2,3];
var out = [];
for(var i = 0; i<arr.length;i++) {
let item = arr[i]; // block scoped
out.push(function(){ alert(item); });
}
out.forEach(function(func){ func(); });
testFunc();
var testFunc = function(){ alert("hey!"); };
testFunc();
(function testFunc(){ alert("hey!"); });
testFunc();
function testFunc(){ alert("hey!"); };
(function(){
var hello = "world";
//some other code
var expression = function(){ alert("expression!"); };
var foo = "bar";
function declaration(){ alert("declaration!"); };
})();
(function(){
var foo, hello, expression;
function declaration(){ alert("declaration!"); };
hello = "world";
//some other code
expression = function(){ alert("expression!"); };
foo = "bar";
})();
function declaration(){}
(function expression(){
function declaration(){}
}())
var expression = function(){}; // assignment expression
var expression = function expression(){};
(function expression(){})(); // () is the grouping operator
new function expression(){}; // new expression
0, function expression(){}; // the comma is an operator
void function expression(){}
+function expression(){}
Only easy rule - if there's no name, it's a expression
If there is a name, it depends on context
this
can be set in