On Github maxbeizer / undefined-is-not-a-function
or what the most common errors
in JavaScript really mean
by Max Beizer / @maxbeizer
var x;
int x;
(num|string) + (num|string); // awesome sauce (maybe)
[] + []; // ???
[] + {}; // ???
[] + []; // ""
[] + {}; // "[object Object]"
[] + []; ""
[] + {}; "[object Object]"
var array = [];
array.valueOf() === array; // true, same for object
toString([]); // ""
toString({}); // "[object Object]"
var myWeight = '190'; // were only wishing made that so
myNum + 50; // more like it
if(peaceTreaty === 'undefined') {
launchZeeMissiles();
}
if(typeof peaceTreaty === 'undefined') {
launchZeeMissiles();
}
NaN === NaN; // ???
NaN === NaN; // false
isNaN(NaN); // true
var mySoCalledNum = Number("90s ftw"); // returns NaN
// but not itself NaN
isNaN(mySoCalledNum); // true -> implicit conversion
var myBool = 'false';
myBool === true; // ???
myBool === false; // ???
var myBool = 'false';
myBool === true; // false
myBool === false; // false
var myBool = 'false';
if(myBool === false) { // typeof myBool === 'string'
partyInTheUSA();
} else {
theEndIsNigh = true;
}
var obj = {
doAllTheThings: function() {
console.log(this);
}
};
obj.doAllTheThings() === obj; // ???
var obj = {
doAllTheThings: function() {
console.log(this);
}
};
obj.doAllTheThings() === obj; // true
function funkyFunc() {
console.log(this);
}
funkyFunc(); // ???
function somewhatFunkyFunc() {
'use strict';
console.log(this);
}
somewhatFunkyFunc(); // ???
function funkyFunc() {
console.log(this);
}
funkyFunc(); // window
function somewhatFunkyFunc() {
'use strict';
console.log(this);
}
somewhatFunkyFunc(); // undefined
var thisHuh;
function MyObject() {
thisHuh = this;
}
var instance = new MyObject();
console.log(thisHuh === instance); // ???
function newOperator(Constr, arrayWithArgs) {
var thisValue = Object.create(Constr.prototype);
Constr.apply(thisValue, arrayWithArgs);
return thisValue;
}
h/t Dr. Axel Rauschmayer, again
var lloydChristmas = {};
lloydChristmas.harryDunne(); // undefined is not a function
no reference to harryDunne
var underfunded;
underfunded.manDate;
// TypeError: Cannot read property 'manDate' of undefined
undefined = 'anything';
var smarm = {
aString: 'theory',
aNum: 42,
aBool: true,
doFunc: function () {
var a = 'bob';
console.log(this); // smarm
(function () {
console.log(this); // window - 'this' is reset
console.log(a); // 'bob' - still in scope
}());
}
};
smarm.doFunc();
var smarm = {
aString: 'theory',
aNum: 42,
aBool: true,
doFunc: function () {
var a = 'bob';
console.log(this); // smarm
(function () {
console.log(this); // window - 'this' is reset
console.log(a); // 'bob' - still in scope
}());
}
};
smarm.doFunc();
var smarm = {
aString: 'theory',
aNum: 42,
aBool: true,
doFunc: function () {
var a = 'bob';
console.log(this); // smarm
(function () {
console.log(this); // window - 'this' is reset
console.log(a); // 'bob' - still in scope
}());
}
};
smarm.doFunc();
var Developer = function() {
this.back = 'bad';
this.posture = 'worse';
this.drinkCoffee = function() {
alert('I am on top of the world');
};
};
var Developer = function() {
this.back = 'bad';
this.posture = 'worse';
};
Developer.prototype.drinkCoffee = function() {
alert('I believe I can fly');
};
typeof happiness !== 'undefined'