At the end of this class, you will be able to:
wilson-espina/jsd-9-resources
repo to your computer:cd
to the Documents/JSD/jsd-9-resources
directorygit pull origin master
and press return
Documents/JSD/jsd-9-resources/06-objects-json
var
are lifted to the top of their function/local scope.Code written by developer
function celebrate() {
console.log("Wahoo!");
var x = 1;
}
Code as intrerpreted by parser
function celebrate() {
var x; // top of function scope
console.log("Wahoo!");
x = 1;
}
var
are hoisted, but not their values.Code written by developer
function celebrate() {
console.log("Wahoo!");
let x = 1;
}
Code as intrerpreted by parser
function celebrate() {
console.log("Wahoo!");
let x = 1;
}
let
or const
are not hoisted.Code written by developer
foo()
function foo() {
console.log("Hi!");
}
Code as intrerpreted by parser
function foo() {
console.log("Hi!");
}
foo();
Code written by developer
foo()
var foo = function() {
console.log("Hi!");
}
Code as intrerpreted by parser
var foo;
foo(); // error foo is not a function
foo = function() {
console.log("Hi!");
}
Code written by developer
foo()
let foo = function() {
console.log("Hi!");
}
Code as intrerpreted by parser
foo(); // foo is not defined
let foo = function() {
console.log("Hi!");
}
Keyword | Scope | Can Be Reassigned | Hoisting |
---|---|---|---|
var | Function scope | Yes | Yes |
let | Block scope | Yes | No |
const | Block scope | No | No |
const myObject = new Object();
const myObject = {};
// Most common way is 'object literal syntax'
null
or undefined
and even another object.
[
"Dave", // 0
"Benson-Phillips", // 1
55 // 2
]
Array
Ordered
{
name: "Dave",
lastName: "Benson-Phillips",
age: 55
}
Object
Not ordered
const animal = {
type: 'dog',
speak: function() {
console.log("Woof!");
}
}
const person = {
name: 'Wilson',
location: 'London',
shout: function() {
console.log("I hate lockdown");
}
};
person.name // getting a value
=> "Wilson"
person.name = 'Steven'; // setting a value
=> "Steven"
person.shout() // calling a method
=> "I hate lockdown"
const person = {
name: 'Wilson',
location: 'London',
shout: function() {
console.log("I hate lockdown");
}
};
person['name']
=> "Wilson"
person['name'] = 'Steven';
=> "Steven";
person['whisper'] = function() {
console.log("Social distancing is my jam");
};
const name = 'Jayne';
const object = {
name: name
};
object;
=> { name: "Jayne" }
const object = { name }; // ES6 syntax
const name = 'Adrianna';
const nationality = 'Polish';
const dob = '1990-10-10';
const person = { name, nationality, dob };
person;
=> Object { name: "Adrianna", nationality: "Polish", dob: "1990-10-10" };
const person = { name: 'Adrianna', nationality: 'Polish', dob: '1990-10-10' };
const { name, dob, nationality } = person;
name;
=> "Adrianna";
nationality;
=> "Polish";
dob;
=> "1990-10-10";
const classroom = {
name: 'JSD 9',
type: 'Remote',
start: '11/05/2020'
};
delete classroom.start;
classroom
=> Object { name: "WDI 2", type: "Remote" }
Open up: 1-object-codealong
Key Objective
Type of Exercise
Location
Timing
5 mins |
|
"A user, browsing on a shopping website, searches for size 10 running shoes, and examines several pairs before purchasing one."
"A user, browsing on a shopping website, searches for size 10 running shoes, and examines several pairs before purchasing one."
implicit object:
shopping cart
"A user, browsing on a shopping website, searches for size 10 running shoes, and examines several pairs before purchasing one."
implicit properties:
for each pair of shoes:
price
colour
for the shopping cart:
contents
total
shipping
"A user, browsing on a shopping website, searches for size 10 running shoes, and examines several pairs before purchasing one."
implicit properties:
for each pair of shoes:
add to cart
for the shopping cart:
calculate shipping
calculate tax
complete purchase
remove item
Key Objective
Type of Exercise
Location
Timing
15 mins |
|
Object
const course = {
name: 'JSD',
weeks: 10,
location: 'Remote',
instructor; 'Wilson Espina',
students: [
'Mr Motivator',
'Cathy Burke',
'Carl Cox',
'Steven King',
'Susie Dent'
],
dates: {
start: '2020-05-11',
end: '2020-07-20'
}
}
JSON
const course = {
"name": "JSD",
"weeks": 10,
"location": "Remote",
"instructor": "Wilson Espina",
"students": [
"Mr Motivator",
"Cathy Burke",
"Carl Cox",
"Steven King",
"Susie Dent"
],
"dates": {
"start": "2020-05-11",
"end": "2020-07-20"
}
}
const course = {
"name": "JSD",
"weeks": 10,
"location": "Remote",
"instructor": "Wilson Espina",
"students": [
"Mr Motivator",
"Cathy Burke",
"Carl Cox",
"Steven King",
"Susie Dent"
],
"dates": {
"start": "2020-05-11",
"end": "2020-07-20"
}
}
"Programme": {
"title": "Coronation Street",
"synopsis":
"Geoff forces Yasmeen into an embarrassing situation in the Rovers.",
"image":
"https://images-itv.com/episode/8290/1019",
"description":
"Geoff forces Yasmeen into an embarrassing situation in the Rovers.",
"dateTime": "2020-05-01T18:30:00.000Z",
"channel": "itv",
"production":
{ "productionId": "8290/1019/11",
"productionType": "PROGRAMME"
}
}
{}
:
and separated by a comma,
.""
Boolean
values can either be true
or false
and NULL values are represented with null
(without quotes).
JSON.stringify(object);
JSON.parse(json);
Open up: 4-json-codealong
Key Objective
Type of Exercise
Location
Timing
10 mins |
|
const course = '{"name":"JSD","weeks":
10,"location":"Remote","instructor":
"Wilson Espina","students":["Mr
Motivator","Cathy Burke","Carl Cox",
"Steven King","Susie Dent"],"dates":
{"start":"2020-05-11","end":"2020-07-
20"}}'
Open up: 6-cat-facts-json
Key Objective
Type of Exercise
Location
Timing
8 mins |
|
(Lesson #06)