This document provides an overview of JavaScript basics including data types, dynamic typing, strings, numbers, Booleans, arrays, objects, functions, scope, and common patterns like namespaces, modules, and immediately invoked function expressions (IIFEs). It covers topics like primitive vs reference types, type coercion, equality operators, object literals, constructors, and the revealing module pattern for encapsulating code.
15. Objects
•
•
•
•
•
Everything is an Object
Booleans can be objects or primitive data treated as objects
Numbers can be objects or primitive data treated as objects
Strings are also objects or primitive data treated as objects
Dates, Maths, Regular expressions, Arrays and functions are always objects
16. Object literal
An object is just a special kind of data,
with properties and methods.
var person = {
firstName: "John",
lastName: "Doe",
id: 5
};
17. Object literal
An object is just a special kind of data,
with properties and methods.
var person = {
firstName: "John",
lastName: "Doe",
id: 5
};
person.id; // 5
18. Object literal
An object is just a special kind of data,
with properties and methods.
var person = {
firstName: "John",
lastName: "Doe",
address: {
street: "Strandsvingen",
number: "14B"
}
};
person.address.street; // "Strandsvingen"
19. Functions
a block of code that will be executed when "someone" calls it:
function add(a, b) {
return a + b;
}
var add = function(a, b) {
return a + b;
}
23. Type coercion
• When JavaScript are unsure what you mean, It makes a guess
• Example:
if ('false') { alert("true"); }
• «A String is obviously not true or false, you probably
mean true!»
24. True!
• if (true) { alert("true"); } // alerts "true"
• if ('false') { alert("true"); } // alerts "true"
• if ([]) { alert("true"); } // alerts "true"
• if (5) { alert("true"); } // alerts "true"
• if ({}) { alert("true"); } // alerts "true"
25. False
• if (false) { alert("false"); } // does nothing
• if (0) { alert("false"); } // does nothing
• if (null) { alert("false"); } // does nothing
• if (undefined) { alert("false"); } // does nothing
• if ("") { alert("false"); } // does nothing
35. Scope & global variables
• C#/Java: anything inside curly brackets, {} , defines a scope
• Example:
if (true)
{
var scopeVariable = "Test";
}
scopeVariable = "Test2"; // variable not defined
36. Scope & global variables
• Javascript: only functions define a new scope
• Example:
if (true) {
var scopeVariable = "Test";
}
scopeVariable; // value: "Test";
37. Scope & global variables
function scope1() {
var member; //is a member of the scope defined by the function example
//this function is also part of the scope of the function example
var innerScope = function() {
member= 12; // traverses scope and assigns member in scope1 to 12
};
};
38. Scope & global variables
function scope1() {
var member; //is a member of the scope defined by the function example
//this function is also part of the scope of the function example
var innerScope = function() {
var member= 12; // defines member in this scope and do not traverse
};
};
39. Scope & global variables
function scope1() {
var member;
//is a member of the scope defined by the function example
//this function is also part of the scope of the function example
var bar = function() {
member= 12; // traverses scope and assigns scope1member.foo to 12
};
};
function scope2() {
member = 15; // traverses scope and assigns global.member to 15
}
48. Why?
• Executes an expression and therefore code immediately
• Creates «privacy» for your code (function scope ftw!)
49. How?
• JavaScript, parenthesis can’t contain statements.
• When the parser encounters the function keyword, it knows to
parse it as a function expression and not a function declaration.
50. Immediately invoked function expressions
•
•
•
•
•
(function () {
// logic/code here
})();
The key thing about JavaScript expressions is that they return values.
To invoke the function expression right away we just need to tackle a couple
of parentheses on the end(like above).