JavaScript has dynamic types where variables can change type, like a variable starting as undefined then being set to a number or string. It supports basic types like strings, numbers, booleans, and arrays. Arrays can be accessed and modified using methods. Objects are used to group related data and code, and functions are code blocks that execute when called. Namespaces and immediately invoked function expressions can help organize code and provide privacy.
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') { console.log("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
}
45. Why?
• Executes an expression and therefore code immediately
• Creates «privacy» for your code (function scope ftw!)
46. 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.
47. 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).