Andrew Krawchyk
3/14/15
General Assembly
Values that are restricted from change; immutable.
Or "voyeur" in this case. No truly private vars until ES7.
Convention is for camelCasing in JS
var Person = (function() {
var sharedName;
function Person(name) {
sharedName = name;
}
Person.prototype.getName = function() {
return sharedName;
};
return Person;
}());
Shared values across all instances:
s = new Person('steve');
s.getName(); // 'steve'
a = new Person('art');
s.getName(); // 'art'
var str = '55';
var num = 55;
console.log(str + num);
// '5555'
var pop = 'a long time';
function setPop(person) {
person.populraity = pop;
}
var dynamicVariable = 3;
Number
dynamicVariable = '3';
String
dynamicVariable = true;
Boolean
dynamicVariable = null;
dynamicVariable
undefined
Null
var dynamicVariable = 3;
"number"
dynamicVariable = '3';
"string"
dynamicVariable = true;
"boolean"
dynamicVariable = null;
dynamicVariable
"undefined"
"object"
JavaScript has a data type, NaN, which represents everything "not a number."
There is an important but subtle difference between null and undefined.
Do not test for NaN, use the built-in function isNaN(val).
Also feel free to use typeof.
First, typeof null is "object."
Second, when comparing:
null == undefined; // true
but...
null === undefined: // false
undefined is the value automatically assigned to all variables that are not declared or are declared without a value assignment.
Curiously it's not a reserved word:
(function(undefined) {
console.log(undefined);
})('why?');
You cannot change the value of undefined - it is a built-in called a primitive.
var favorites;
console.log(abc);
// ReferenceError abc is not defined
typeof abc === 'undefined'
// true
true
1
'true'
'false'
[]
{}
function() {}
false
0
undefined
NaN
'' // that's the empty string
null
Strings are easy to use to represent complex data. This can often be helpful in the short term, for example adding a separator between values to store names. Works until you come across a name with a comma (for whatever reason).
.toString('asdf');
val + 'asdf';
parseInt('5', 10);
parseFloat('3.14');
var jobsCountRemaining = 2;
// ternary
var completed = jobsCountRemaining === 0 ? true : false;
return completed;
// if-else
if (jobsCountRemaining === 0) {
completed = true;
} else {
completed = false;
}
return completed;
or why you always use ===
Equality tests the coerced value:
0 == '0' // true
1 == 2 // false
Identity tests equality as well as type of the value:
0 === '0' // false
1 === 2 // false
Easy way to enumerate collections in JS
var statusesEnum = {
LIKED: 'liked',
DISLIKED: 'disliked',
BLOCKED: 'blocked',
REPORTED: 'reported'
};
var alertColorsEnum = {
INFO: '#00FF2F',
WARNING: '#FFFB00',
ERROR: '#FF8800',
CRITICAL: '#FF0000'
}
Can be repetitive, however it defines a commonly used thing in an application
Worth the small amount of repetition to have a single source of truth for these values
var statusesEnum = {
LIKED: 'liked',
DISLIKED: 'disliked',
INVISIBLE: 'invisible',
PRIVATE: 'private',
BLOCKED: 'blocked',
REPORTED: 'reported'
};
Can easily add new statuses
Can easily recognize previously added statuses
var statusesEnum = {
LIKED: 'liked',
UPVOTED: 'upvoted',
DISLIKED: 'disliked',
DOWNVOTED: 'downvoted',
BLOCKED: 'blocked',
REPORTED: 'reported'
};