Alex Dajani
JavaScript Evangelist
Maker Square
slides: http://bit.ly/1BH0GOx
exercises: bit.ly/js102-exercises
2-6mos experience learning JavaScript
Completed JS tutorial like Codecademy, CodeSchool, Khan Academy, or Bloc.io
Want to solidify fundamentals
Looking for more practice
Interested in getting into MakerSquare
@AlexDajani
Review and solidify core JS principles with project.
Get started with functional methods.
Use foundation to grow as a JS engineer.
@AlexDajani
{}
@AlexDajani
var box = {};
@AlexDajani
var box = {};
box.material = "cardboard";
When are some other times you have seen dots in JS?
var box =
{"material" : "cardboard"}
@AlexDajani
var box = {};
box.material = "cardboard";
box.material; //??
var box =
{"material" : "cardboard"}
@AlexDajani
var box = {};
box.material = "cardboard";
var cb = box.material;
var box =
{"material" : "cardboard"}
@AlexDajani
box =
{"material" : "cardboard"}
var box = {};
box.material = "cardboard";
var cb = box.material;
cb; //??
box.material = "titanium";
cb; //??box =
{"material" : "titanium"}
cb = "cardboard"
@AlexDajani
var box = {};
box.material = "cardboard";
box.material; //"cardboard"
box.size; //??
var box =
{"material" : "cardboard"}
@AlexDajani
var box = {};
box["material"] = "cardboard";
box.material; //??
var box =
{"material" : "cardboard"}
@AlexDajani
var box = {};
box["material"] = "cardboard";
box["material"]; //??
var box =
{"material" : "cardboard"}
@AlexDajani
var box = {};
box["material"] = "cardboard";
var key = "material";
box[key]; //??
@AlexDajani
var box = {};
box["material"] = "cardboard";
var func = function(){
return "material";
};
box[func()]; //??
@AlexDajani
var box = {};
box['material'] = "cardboard";
var key = 'material';
box['key']; //??
var box = {};
box['material'] = "cardboard";
var key = 'material';
box['key']; //undefined
box.key; //??
var box = {};
box['material'] = "cardboard";
var key = 'material';
box['key']; //undefined
box.key; //undefined
var box = {};
box['material'] = "cardboard";
var key = 'material';
box['key']; //undefined
box.key; //undefined
box[key]; //??
var box = {};
box['material'] = "cardboard";
var key = 'material';
box['key']; //undefined
box.key; //undefined
box[key]; //"cardboard"
@AlexDajani
var box = {};
box['material'] = "cardboard";
box[0] = 'meow';
box['^&*'] = "testing 123";
var test = box['^&*'];
box =
{"material" : "cardboard",
"0" : "meow",
"^&*" : "testing 123"
}
@AlexDajani
DOTS
strings
Brackets
"strings
"quotes required
"weird characters
variables
numbers
numbers
quotations
weird characters
expressions
expressions
@AlexDajani
var box = {};
box["material"] = "cardboard";
box["size"] = {
"height": 2,
"width": 80
};
box.area = function(){
return box.size.height * box.size.width;
};
@AlexDajani
@AlexDajani
var box = {};
box['size'] = 9;
box['~/ [."4'] = 'meow';
box['size']; // 9
box['~/ [."4']; // 'meow'
var box = { box['size'] = 9; box['~/ [."4'] = 'meow';};box['size']; // 9 box['~/ [."4']; // 'meow'
var box = {};box['size']= 9;box['~/ [."4']= 'meow';box['size']; // 9 box['~/ [."4']; // 'meow'
var box = { 'size' = 9; '~/ [."4' = 'meow';};box['size']; // 9 box['~/ [."4']; // 'meow'
var box = { 'size' : 9, '~/ [."4' : 'meow';};box['size']; // 9 box['~/ [."4']; // 'meow'
var box = { 'size' : 9, '~/ [."4' : 'meow'};box['size']; // 9 box['~/ [."4']; // 'meow'
@AlexDajani
var box = {};
box['material'] = "cardboard";
box[0] = 'meow';
box['^&*'] = "testing 123";
for(var key in box){
console.log(key); //??
}
@AlexDajani
var box = {};
box['material'] = "cardboard";
box[0] = 'meow';
box['^&*'] = "testing 123";
for(var key in box){
console.log(box[key]); //??
}
@AlexDajani
@AlexDajani
What is an object?
What is the difference between dot and bracket notation?
When can you only use brackets and not dot?
When can you only use dot notation and not bracket?
How do you add a property with a key that contains special characters?
How do you add a property with a key that is stored in a variable?
How do you add a property whose key and value are stored in different variables?
How do you access an object that is inside another object?
How do you create an object that is nested inside another object?
How do we loop through objects to access the values?
[]
@AlexDajani
@AlexDajani
var box = [];
box[0] = true;
box[1] = 'meow';
box.push({'hello' : 'goodbye'});
var i = 0;
box[i]; // ??
box[1];
box.pop() //??
[true, 'meow', {'hello' : 'goodbye'}]
index# 0 1 2
{0 : 'meow', 'size' : 9 }
@AlexDajani
var box = {};
box['size'] = 9;
box['0'] = 'meow';
box['size']; // 9
box[0]; // 'meow'
var box = [];
box['size'] = 9;
box['0'] = 'meow';
box['size']; // ??
box[0]; // ??
['meow']
index # 0
{0 : 'meow', 'size' : 9 }
@AlexDajani
var box = [];
box['size'] = 9;
box['0'] = 'meow';
box.size; // ??
box[0]; // ??
['meow']
index # 0
{0 : 'meow', 'size' : 9 }
@AlexDajani
var box = [];
box['size'] = 9;
box['0'] = 'meow';
box.size; // ??
box.0; // ??
['meow']
index # 0
{0 : 'meow', 'size' : 9 }
@AlexDajani
var box = [];
box['size'] = 9;
box['0'] = 'meow';
for(var k in box){
console.log(k); // ??
}
['meow']
index # 0
{0 : 'meow', 'size' : 9 }
@AlexDajani
var box = [];
box['size'] = 9;
box['0'] = 'meow';
for(var k in box){
console.log(box.k); // ??
}
['meow']
index # 0
{0 : 'meow', 'size' : 9 }
@AlexDajani
var box = [];
box['size'] = 9;
box['0'] = 'meow';
for(var k in box){
console.log(box[k]); //??
}
['meow']
index # 0
{0 : 'meow', 'size' : 9 }
@AlexDajani
var box = [];
box['size'] = 9;
box['0'] = 'meow';
box.push('Whoohoo!');
for(var k in box){
console.log(box[k]);
}
for(var i = 0; i < box.length; i++){
console.log(i); //??
}
['meow', 'Whoohooo!']
index # 0 1
{0 : 'meow', 1 : 'Whoohooo!', 'size' : 9 }
@AlexDajani
var box = [];
box['size'] = 9;
box['0'] = 'meow';
box.push('Whoohoo!');
for(var i = 0; i < box.length; i++){
console.log(box.i); //??
}
['meow', 'Whoohooo!']
index # 0 1
{0 : 'meow', 1 : 'Whoohooo!', 'size' : 9 }
@AlexDajani
var box = [];
box['size'] = 9;
box['0'] = 'meow';
box.push('Whoohoo!');
for(var i = 0; i < box.length; i++){
console.log(box[i]); //??
}
['meow', 'Whoohooo!']
index # 0 1
{0 : 'meow', 1 : 'Whoohooo!', 'size' : 9 }
@AlexDajani
var box = [];
box['size'] = true;
box['0'] = 'meow';
box.length; //??
['meow']
index # 0
{0 : 'meow', 'size' : 9 }
@AlexDajani
var box = [];
box['0'] = 'meow';
box[3] = {'babyBox': true};
box['length']; //??
@AlexDajani
var box = [];
box['0'] = 'meow';
box[3] = {'babyBox': true};
box[length]; //??
@AlexDajani
var box = [];
box['0'] = 'meow';
box[1] = 'Whoohooo!';
box[3] = {'babyBox': true};
box[box.length]; //??
@AlexDajani
var box = [];
box['size'] = 9;
box['0'] = 'meow';
box[1] = 'Whoohooo!';
box[box.length - 1]; //??
['meow', 'Whoohooo!']
index # 0 1
{0 : 'meow', 1 : 'Whoohooo!', 'size' : 9 }
@AlexDajani
var box = [];
box['0'] = 'meow';
box[1] = 'Whoohooo!';
box[box.length - 1]; //??
['meow', 'Whoohooo!']
index # 0 1
{0 : 'meow', 1: 'Whoohooo!'}
@AlexDajani
var box = {};
box.innerBox = {};
var box =
{"innerBox" : {}}
@AlexDajani
var box = {};
box['innerBox'] = {};
var box =
{"innerBox" : {}}
@AlexDajani
var box = {};
box['innerBox'] = {};
box['innerBox']['full'] = true;
var box =
{"innerBox" : {
full: true
}
}
@AlexDajani
var box = {};
box['innerBox'] = {};
box['innerBox'].full = true;
var box =
{"innerBox" : {
full: true
}
}
@AlexDajani
var box = {};
box.innerBox = {};
box.innerBox.full = true;
box.innerBox; //??
var box =
{"innerBox" : {
full: true
}
}
@AlexDajani
var box = {};
box.innerBox = {};
box.innerBox.full = true;
var myInnerBox = box.innerBox;
myInnerBox; //??
var box =
{"innerBox" : {
full: true
}
}
@AlexDajani
var box = {};
box.innerBox = {};
box.innerBox.babyBox = {};
box.innerBox['babyBox']; //??
box.innerBox['babyBox'].says = "What's up!?";
var box =
{"innerBox" : {
babyBox : {
says: "What's up!?"
}
}
}
@AlexDajani
var box = {};
box['innerBox'] = {};
box['innerBox'].full = true;
box[innerBox]['height'] = 10;
box[innerBox2].full = false;
@AlexDajani
@AlexDajani
What do you use to loop through an array?
What are some different methods of adding values to an array?
What are some special properties about arrays that are different from objects?
What are some ways to access values in your array?
When would you use an array over an object?
function(){}
@AlexDajani
@AlexDajani
var nameImprover = function (name, adj) {
return 'Col ' + name + ' Mc' + adj + ' pants';
};
$('body').hide();
myArr.forEach(function(val){ console.log(val);});
$('button').on('click', function(){
console.log('Don\'t press my buttons!');
});
@AlexDajani
var nameImprover = function (name, adj) {
return 'Col ' + name + ' Mc' + adj + ' pants';
};
$('body').hide();
myArr.forEach(function(val){ console.log(val);});
$('button').on('click', function(){
console.log('Don\'t press my buttons!');
});
@AlexDajani
var nameImprover = function (name, adj) {
return 'Col ' + name + ' Mc' + adj + ' pants';
};
$('body').hide();
myArr.forEach(function(val){ console.log(val);});
$('button').on('click', function(){
console.log('Don\'t press my buttons!');
});
@AlexDajani
var nameImprover = function (name, adj) {
return 'Col ' + name + ' Mc' + adj + ' pants';
};
$('body').hide();
myArr.forEach(function(val){ console.log(val);});
$('button').on('click', function(){
console.log('Don\'t press my buttons!');
});
@AlexDajani
var nameImprover = function (name, adj) {
return 'Col ' + name + ' Mc' + adj + ' pants';
};
$('body').hide();
myArr.forEach(function(val){ console.log(val);});
$('button').on('click', function(){
console.log('Don\'t press my buttons!');
});
@AlexDajani
var nameImprover = function (name, adj) {
return 'Col ' + name + ' Mc' + adj + ' pants';
};
$('body').hide();
myArr.forEach(function(val){ console.log(val);});
$('button').on('click', function(){
console.log('Don\'t press my buttons!');
});
@AlexDajani
var add = function(a, b){
return a + b;
};
add(3, 4, 5); // ??
add(4, 10, 3); //13?
@AlexDajani
var add = function(a, b){
console.log(arguments); //logs [3,10,5]
return a + b;
};
add(3, 10, 5); //13
@AlexDajani
var add = function(a, b){
console.log(arguments); //logs [3,10,5]
return a + b;
};
add(3, 10, 5); //18??
@AlexDajani
var add = function(a, b){
arguments.slice(0,1) //ERROR!!
return a + b;
};
add(3, 10, 5);
@AlexDajani
var add = function(a, b){
return a + b;
};
add.example = 'testing 123!';
@AlexDajani
@AlexDajani
@AlexDajani
function AnimalMaker(name) {
return {
speak: function () {
console.log("my name is ", name);
}
};
};
var animalNames = ['Sheep', 'Liger', 'Big Bird'];
@AlexDajani
function AnimalMaker(name) {
return {
speak: function () {
console.log("my name is ", name);
}
};
};
var animalNames = ['Sheep', 'Liger', 'Big Bird'];
var farm = [];
@AlexDajani
function AnimalMaker(name) {
return {
speak: function () {
console.log("my name is ", name);
}
};
};
var animalNames = ['Sheep', 'Liger', 'Big Bird'];
var farm = [];
for(var i = 0; i < animalNames.length; i++){
var animal = AnimalMaker(animalNames[i])
farm.push(animal);
}
@AlexDajani
function AnimalMaker(name) {
return {
speak: function () {
console.log("my name is ", name);
}
};
};
var animalNames = ['', '', ''];
var farm = [];
for(var i = 0; i < animalNames.length; i++){
farm.push(AnimalMaker(animalNames[i]));
//prev 2 lines condensed onto one
}
@AlexDajani
function AnimalMaker(name) {
return {
speak: function () {
console.log("my name is ", name);
}
};
};
var animalNames = ['Sheep', 'Liger', 'Big Bird'];
var farm = [];
for(var i = 0; i < animalNames.length; i++){
farm.push(AnimalMaker(animalNames[i]));
}
for(var j = 0; j < farm.length; j++){
farm[j].speak()
}
@AlexDajani
@AlexDajani
var func = function(){
var local = true;
};
console.log(local);@AlexDajani
var x = 'global!';var x = 'global!';
//inside a function
function encapsulate(){
z = 'global here, too!';
}var x = 'global!';
//inside a function
function encapsulate(){
z = 'global here, too!';
window.y = 'also global!';
}
@AlexDajani
var g = 'global';
function blender(fruit) {
var b = fruit;
var y = 'yogurt';
function bs() {
alert( b + ' and ' + y + ' makes ' + b + ' swirl');
}
bs();
}
blender('blueberry');
@AlexDajani
var g = 'global';
function blender(fruit) {
var b = fruit;
var y = 'yogurt';
function bs() {
alert( b + ' and ' + y + ' makes ' + b + ' swirl');
}
bs();
}
blender('blueberry');
@AlexDajani
@AlexDajani
var g = "global";
function go() {
var l = "local";
var g = "in here!";
alert(g + " inside go");
}
go();
alert(g + " outside go");@AlexDajani
var inBlock = false;
for(var i = 0; i < 5; i++){
var inBlock = true;
};
if(inBlock){
console.log('Is there block scope? ' + !inBlock);
}@AlexDajani
@AlexDajani
@AlexDajani
Check Dates here