Created
November 7, 2017 16:56
-
-
Save alcance/c1e9aca8b0b06cfd483cc3d04f474697 to your computer and use it in GitHub Desktop.
JS Bin // source http://jsbin.com/qafice
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
// Create a variable name and assign | |
// it an object literal. | |
var animal = {}; | |
/* | |
* Dot Notation | |
*/ | |
animal.username = 'pig'; | |
/* | |
* Bracket notation | |
*/ | |
animal['tagline'] = 'hotdog'; | |
/* | |
* Add noises array to object | |
*/ | |
var noises = []; | |
animal.noises = noises; | |
/* | |
* Output | |
*/ | |
// console.log(animal); | |
/* | |
* Loops | |
*/ | |
var count = 0; | |
for (var key in animal) { | |
count++ | |
if (key === 'username') { | |
console.log('Hi my name is ' + animal[key]); | |
} else if (key === 'tagline') { | |
console.log('I like to say ' + animal[key]); | |
} | |
} | |
/* | |
* Chapter II | |
* Array exercises | |
*/ | |
var noiseArray = ['meow']; | |
// add item to the beginning | |
noiseArray.unshift('guau'); | |
// add item to the end | |
noiseArray.push('w00t'); | |
/* | |
* Add item to the end using | |
* Brackets notation | |
*/ | |
noiseArray[noiseArray.length] = 'boow'; | |
console.log(noiseArray.length); | |
//console.log(noiseArray); | |
// Add noiseArray to animal object on | |
// noise propertie | |
animal.noises = noiseArray; | |
//console.log(animal); | |
/* | |
* Animal collection | |
*/ | |
var animals = []; | |
// Add the animal object to animals array | |
animals.push(animal); | |
// Assing quackers object and assign it | |
var quackers = { | |
username: 'DaffyDuck', | |
tagline: 'Yippeee!', | |
noises: ['quack', 'honk', 'sneeze', 'growl'] | |
} | |
// Create two more animals and | |
// push it to array | |
var camel = { | |
username: 'Watuki', | |
tagline: 'Heyyya', | |
noises: ['quick', 'cuiiit', 'quack', 'bow'] | |
}; | |
var kitty = { | |
username: 'Kittykat', | |
tagline: 'Meeeowwoowow', | |
noises: ['meeeaow', 'quiiiiick', 'oooar'] | |
}; | |
animals.push(quackers, camel, kitty) | |
console.log('animals obj', animals); | |
// Check length | |
console.log(animals.length); | |
/* | |
* Functions | |
* encapsulation, part of code | |
* runs something | |
* A function is a blender | |
* You can have a factory that makes blender | |
*/ | |
// Function definition | |
// Function body | |
// Create Animal Maker with loop | |
var AnimalMaker = function AnimalMaker(name) { | |
return { | |
speak: function() { | |
console.log('my name is', name); | |
} | |
} | |
} | |
var myAnimal = AnimalMaker('Wootner'); | |
var animalNames = ['Sheep', 'Tiger', 'Big Bird']; | |
for (i = 0; i < animalNames.length; i++) { | |
console.log(animalNames[i]); | |
} | |
/* Function's Exercise | |
* - Write a function AnimalTestUser | |
* - Has one string param (username) | |
* - Returns an object with username prop | |
*/ | |
var AnimalTestUser = function AnimalTestUser(username) { | |
var otherArgs = []; | |
// Check how many args are passed | |
console.log(arguments.length); | |
// If there is more than one arg create a property otherArgs | |
if (arguments.length >= 1) { | |
// otherArgs is an array of remaining arguments | |
for (var i = 1; i < arguments.length; i++) { | |
otherArgs.push(arguments[i]); | |
} | |
// arguments is not a true array, convert it to array like | |
console.log('otherArgs', otherArgs); | |
} | |
return { | |
username: username, | |
otherArgs: otherArgs | |
} | |
} | |
var testSheep = AnimalTestUser('CottonBall', {'loves dancing': true}, [1,2,3]); | |
console.log(testSheep); | |
/* | |
* Write a contruction function AnimalCreator | |
* - Construction Function has 4 params: username, species, tagline and noises | |
* - Animal object should have 5 props: username, species, noises, tagline and friends | |
* - noises and friends are arrays | |
*/ | |
var AnimalCreator = function AnimalCreator(username, species, tagline, noises) { | |
var animal = { | |
username: username, | |
species: species, | |
tagline: tagline, | |
noises: [], | |
friends: [] | |
} | |
if (noises instanceof Array) { | |
for (i=0; i < noises.length; i++) { | |
animal.noises.push(noises[i]); | |
} | |
} | |
else { | |
alert('Noises must be an array!'); | |
} | |
return animal; | |
} | |
var sheep = AnimalCreator('Cloud', 'sheep', 'You can count on me!', ['baahhh', 'arrgg', 'chewchewchew']) | |
console.log('AnimalCreator', sheep); | |
/* | |
* Write a function addFriend | |
* - Takes an animal object | |
* - Adds another animal object as a friend | |
*/ | |
var cow = AnimalCreator('Moo', 'cow', 'I am cow!', ['mooo', 'woo', 'coooow']) | |
console.log('AnimalCreator Cow', cow); | |
var addFriend = function(animal, friend) { | |
animal.friends.push(friend.username); | |
} | |
addFriend(sheep, cow); | |
// Add second friend | |
var llama = AnimalCreator('Zeny', 'llama', 'Whataa llama!', ['cuiii', 'lelz', 'refs']) | |
addFriend(sheep, llama); | |
// Make sure all animals have friends | |
addFriend(cow, llama); | |
addFriend(llama, sheep); | |
console.log('Sheep', sheep); | |
// Create a farm (myFarm) with 3 animal objects | |
var myFarm = [sheep, cow, llama]; | |
console.log('Your farm', myFarm); | |
/* | |
* Create addMatchesArray function | |
* - Takes an array of animal objects | |
* - Adds a new prop to each animal obj called matches | |
*/ | |
var addMatchesArray = function(animals) { | |
for (var i=0; i < animals.length; i++) { | |
animals[i].matches = []; | |
} | |
} | |
addMatchesArray(myFarm); | |
console.log(myFarm); | |
/* | |
* Create a function giveMatches | |
* - Take array of animal objects | |
* - Selects a name from the friends array | |
* - Adds it to the matches array | |
* - Make sure all animal have friends | |
*/ | |
var giveMatches = function(animals) { | |
for (var i = 0; i < animals.lenght; i++) { | |
var selectedAnimal = animals[Math.floor(Math.random() * animals.length)]; | |
console.log('Selected Animal', selectedAnimal); | |
animals[i].matches.push(selectedAnimal.username); | |
} | |
return animals; | |
} | |
console.log(giveMatches(myFarm)); | |
/* | |
* Nesting | |
*/ | |
var box = { | |
innerbox: {} | |
} | |
console.log('box 1', box); | |
var box2 = {} | |
box2['innerbox'] = {} | |
box2['innerbox']['full'] = true; | |
console.log('box 2', box2); | |
// Tiple nesting | |
/* Assign by value or reference | |
* Objects, arrays and functions are by reference | |
* Primitives like string, boolean, undefined by value | |
*/ | |
var box = {}; | |
box['innerBox'] = {}; | |
box['innerBox'].full = true; | |
/* | |
box: { | |
innerBox: { | |
full: true | |
} | |
} | |
*/ | |
box['innerBox']['height'] = 100; | |
// do it here | |
box.innerBox2 = {}; | |
var innerBox2 = 'innerBox2'; | |
box[innerBox2].full = false; | |
console.log('Box', box); | |
/* | |
* ## Exercises for Nesting ## | |
* Represent relationships between two animals in our colection | |
* App has a friendlist on animal's profile whihc list animal's friends | |
* | |
*/ | |
// Create an array for the list of friend's usernames | |
var friends = []; | |
// Using animal array add two usernames to friends | |
friends.push(animals[0].username, animals[1].username); | |
console.log('Friends', friends); | |
// Create a relationship object | |
var relationships = {}; | |
relationships.friends = friends; | |
// What's the length? | |
console.log('Relationships length', Object.keys(relationships).length); // undefined | |
// Create variable matches and assign it to an empty array | |
var matches = []; | |
// Add it to relationship object | |
relationships.matches = matches; | |
// Add at least one username to matches | |
relationships.matches.push(relationships.friends[0]); | |
// Loop animal collection and add relationship obj to each object | |
for (var i = 0; i < animals.length; i++) { | |
animals[i].relationships = relationships; | |
} | |
// Inspect relationship object | |
console.log('Relationships', relationships); | |
// Inspect animals | |
console.log('Animals', animals) | |
/* | |
* Scope: where variable have meaning | |
* - Created dynamically when in function call time | |
* - Everytime you call a function a scope is created | |
*/ | |
// Local Scope | |
var func = function() { | |
var local = true; | |
} | |
// console.log(local); // err! | |
// Global Scope (window object in browser) | |
var x = 'global'; | |
// You can leave var off and make is global | |
function encapsulate() { | |
z = 'this is global, too!'; | |
window.y = 'this is also global!'; | |
} | |
// Parent vs Child Scope | |
// Which scopes have access to which variables ? | |
// The parent CANNOT access the child scope | |
// Where can we access this variables in this nested scope | |
// This is how you create private variables | |
// Scope is not created until you call function | |
var g = 'global'; | |
function blender(fruit){ | |
var b = fruit; | |
var y = 'yogurt'; | |
function bs() { | |
alert( b + ' and ' + y + ' makes ' + b + ' swirl.'); | |
} | |
bs(); | |
} | |
blender('blueberry'); | |
/* | |
* Precedence | |
* Which variable wins if you have two variables with same name | |
*/ | |
var g = 'global'; | |
function go() { | |
var l = 'local'; | |
var g = 'in here!'; | |
console.log(g + ' inside go'); // in here! inside go | |
} | |
go(); | |
alert(g + ' outside go'); //global outside go | |
var inBlock = false; | |
for (var i = 0; i < 5; i++) { | |
var inBlock = true; | |
} | |
if (inBlock) { | |
console.log('Is there block scope? ' + !inBlock); | |
} | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">// Create a variable name and assign | |
// it an object literal. | |
var animal = {}; | |
/* | |
* Dot Notation | |
*/ | |
animal.username = 'pig'; | |
/* | |
* Bracket notation | |
*/ | |
animal['tagline'] = 'hotdog'; | |
/* | |
* Add noises array to object | |
*/ | |
var noises = []; | |
animal.noises = noises; | |
/* | |
* Output | |
*/ | |
// console.log(animal); | |
/* | |
* Loops | |
*/ | |
var count = 0; | |
for (var key in animal) { | |
count++ | |
if (key === 'username') { | |
console.log('Hi my name is ' + animal[key]); | |
} else if (key === 'tagline') { | |
console.log('I like to say ' + animal[key]); | |
} | |
} | |
/* | |
* Chapter II | |
* Array exercises | |
*/ | |
var noiseArray = ['meow']; | |
// add item to the beginning | |
noiseArray.unshift('guau'); | |
// add item to the end | |
noiseArray.push('w00t'); | |
/* | |
* Add item to the end using | |
* Brackets notation | |
*/ | |
noiseArray[noiseArray.length] = 'boow'; | |
console.log(noiseArray.length); | |
//console.log(noiseArray); | |
// Add noiseArray to animal object on | |
// noise propertie | |
animal.noises = noiseArray; | |
//console.log(animal); | |
/* | |
* Animal collection | |
*/ | |
var animals = []; | |
// Add the animal object to animals array | |
animals.push(animal); | |
// Assing quackers object and assign it | |
var quackers = { | |
username: 'DaffyDuck', | |
tagline: 'Yippeee!', | |
noises: ['quack', 'honk', 'sneeze', 'growl'] | |
} | |
// Create two more animals and | |
// push it to array | |
var camel = { | |
username: 'Watuki', | |
tagline: 'Heyyya', | |
noises: ['quick', 'cuiiit', 'quack', 'bow'] | |
}; | |
var kitty = { | |
username: 'Kittykat', | |
tagline: 'Meeeowwoowow', | |
noises: ['meeeaow', 'quiiiiick', 'oooar'] | |
}; | |
animals.push(quackers, camel, kitty) | |
console.log('animals obj', animals); | |
// Check length | |
console.log(animals.length); | |
/* | |
* Functions | |
* encapsulation, part of code | |
* runs something | |
* A function is a blender | |
* You can have a factory that makes blender | |
*/ | |
// Function definition | |
// Function body | |
// Create Animal Maker with loop | |
var AnimalMaker = function AnimalMaker(name) { | |
return { | |
speak: function() { | |
console.log('my name is', name); | |
} | |
} | |
} | |
var myAnimal = AnimalMaker('Wootner'); | |
var animalNames = ['Sheep', 'Tiger', 'Big Bird']; | |
for (i = 0; i < animalNames.length; i++) { | |
console.log(animalNames[i]); | |
} | |
/* Function's Exercise | |
* - Write a function AnimalTestUser | |
* - Has one string param (username) | |
* - Returns an object with username prop | |
*/ | |
var AnimalTestUser = function AnimalTestUser(username) { | |
var otherArgs = []; | |
// Check how many args are passed | |
console.log(arguments.length); | |
// If there is more than one arg create a property otherArgs | |
if (arguments.length >= 1) { | |
// otherArgs is an array of remaining arguments | |
for (var i = 1; i < arguments.length; i++) { | |
otherArgs.push(arguments[i]); | |
} | |
// arguments is not a true array, convert it to array like | |
console.log('otherArgs', otherArgs); | |
} | |
return { | |
username: username, | |
otherArgs: otherArgs | |
} | |
} | |
var testSheep = AnimalTestUser('CottonBall', {'loves dancing': true}, [1,2,3]); | |
console.log(testSheep); | |
/* | |
* Write a contruction function AnimalCreator | |
* - Construction Function has 4 params: username, species, tagline and noises | |
* - Animal object should have 5 props: username, species, noises, tagline and friends | |
* - noises and friends are arrays | |
*/ | |
var AnimalCreator = function AnimalCreator(username, species, tagline, noises) { | |
var animal = { | |
username: username, | |
species: species, | |
tagline: tagline, | |
noises: [], | |
friends: [] | |
} | |
if (noises instanceof Array) { | |
for (i=0; i < noises.length; i++) { | |
animal.noises.push(noises[i]); | |
} | |
} | |
else { | |
alert('Noises must be an array!'); | |
} | |
return animal; | |
} | |
var sheep = AnimalCreator('Cloud', 'sheep', 'You can count on me!', ['baahhh', 'arrgg', 'chewchewchew']) | |
console.log('AnimalCreator', sheep); | |
/* | |
* Write a function addFriend | |
* - Takes an animal object | |
* - Adds another animal object as a friend | |
*/ | |
var cow = AnimalCreator('Moo', 'cow', 'I am cow!', ['mooo', 'woo', 'coooow']) | |
console.log('AnimalCreator Cow', cow); | |
var addFriend = function(animal, friend) { | |
animal.friends.push(friend.username); | |
} | |
addFriend(sheep, cow); | |
// Add second friend | |
var llama = AnimalCreator('Zeny', 'llama', 'Whataa llama!', ['cuiii', 'lelz', 'refs']) | |
addFriend(sheep, llama); | |
// Make sure all animals have friends | |
addFriend(cow, llama); | |
addFriend(llama, sheep); | |
console.log('Sheep', sheep); | |
// Create a farm (myFarm) with 3 animal objects | |
var myFarm = [sheep, cow, llama]; | |
console.log('Your farm', myFarm); | |
/* | |
* Create addMatchesArray function | |
* - Takes an array of animal objects | |
* - Adds a new prop to each animal obj called matches | |
*/ | |
var addMatchesArray = function(animals) { | |
for (var i=0; i < animals.length; i++) { | |
animals[i].matches = []; | |
} | |
} | |
addMatchesArray(myFarm); | |
console.log(myFarm); | |
/* | |
* Create a function giveMatches | |
* - Take array of animal objects | |
* - Selects a name from the friends array | |
* - Adds it to the matches array | |
* - Make sure all animal have friends | |
*/ | |
var giveMatches = function(animals) { | |
for (var i = 0; i < animals.lenght; i++) { | |
var selectedAnimal = animals[Math.floor(Math.random() * animals.length)]; | |
console.log('Selected Animal', selectedAnimal); | |
animals[i].matches.push(selectedAnimal.username); | |
} | |
return animals; | |
} | |
console.log(giveMatches(myFarm)); | |
/* | |
* Nesting | |
*/ | |
var box = { | |
innerbox: {} | |
} | |
console.log('box 1', box); | |
var box2 = {} | |
box2['innerbox'] = {} | |
box2['innerbox']['full'] = true; | |
console.log('box 2', box2); | |
// Tiple nesting | |
/* Assign by value or reference | |
* Objects, arrays and functions are by reference | |
* Primitives like string, boolean, undefined by value | |
*/ | |
var box = {}; | |
box['innerBox'] = {}; | |
box['innerBox'].full = true; | |
/* | |
box: { | |
innerBox: { | |
full: true | |
} | |
} | |
*/ | |
box['innerBox']['height'] = 100; | |
// do it here | |
box.innerBox2 = {}; | |
var innerBox2 = 'innerBox2'; | |
box[innerBox2].full = false; | |
console.log('Box', box); | |
/* | |
* ## Exercises for Nesting ## | |
* Represent relationships between two animals in our colection | |
* App has a friendlist on animal's profile whihc list animal's friends | |
* | |
*/ | |
// Create an array for the list of friend's usernames | |
var friends = []; | |
// Using animal array add two usernames to friends | |
friends.push(animals[0].username, animals[1].username); | |
console.log('Friends', friends); | |
// Create a relationship object | |
var relationships = {}; | |
relationships.friends = friends; | |
// What's the length? | |
console.log('Relationships length', Object.keys(relationships).length); // undefined | |
// Create variable matches and assign it to an empty array | |
var matches = []; | |
// Add it to relationship object | |
relationships.matches = matches; | |
// Add at least one username to matches | |
relationships.matches.push(relationships.friends[0]); | |
// Loop animal collection and add relationship obj to each object | |
for (var i = 0; i < animals.length; i++) { | |
animals[i].relationships = relationships; | |
} | |
// Inspect relationship object | |
console.log('Relationships', relationships); | |
// Inspect animals | |
console.log('Animals', animals) | |
/* | |
* Scope: where variable have meaning | |
* - Created dynamically when in function call time | |
* - Everytime you call a function a scope is created | |
*/ | |
// Local Scope | |
var func = function() { | |
var local = true; | |
} | |
// console.log(local); // err! | |
// Global Scope (window object in browser) | |
var x = 'global'; | |
// You can leave var off and make is global | |
function encapsulate() { | |
z = 'this is global, too!'; | |
window.y = 'this is also global!'; | |
} | |
// Parent vs Child Scope | |
// Which scopes have access to which variables ? | |
// The parent CANNOT access the child scope | |
// Where can we access this variables in this nested scope | |
// This is how you create private variables | |
// Scope is not created until you call function | |
var g = 'global'; | |
function blender(fruit){ | |
var b = fruit; | |
var y = 'yogurt'; | |
function bs() { | |
alert( b + ' and ' + y + ' makes ' + b + ' swirl.'); | |
} | |
bs(); | |
} | |
blender('blueberry'); | |
/* | |
* Precedence | |
* Which variable wins if you have two variables with same name | |
*/ | |
var g = 'global'; | |
function go() { | |
var l = 'local'; | |
var g = 'in here!'; | |
console.log(g + ' inside go'); // in here! inside go | |
} | |
go(); | |
alert(g + ' outside go'); //global outside go | |
var inBlock = false; | |
for (var i = 0; i < 5; i++) { | |
var inBlock = true; | |
} | |
if (inBlock) { | |
console.log('Is there block scope? ' + !inBlock); | |
} | |
</script></body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create a variable name and assign | |
// it an object literal. | |
var animal = {}; | |
/* | |
* Dot Notation | |
*/ | |
animal.username = 'pig'; | |
/* | |
* Bracket notation | |
*/ | |
animal['tagline'] = 'hotdog'; | |
/* | |
* Add noises array to object | |
*/ | |
var noises = []; | |
animal.noises = noises; | |
/* | |
* Output | |
*/ | |
// console.log(animal); | |
/* | |
* Loops | |
*/ | |
var count = 0; | |
for (var key in animal) { | |
count++ | |
if (key === 'username') { | |
console.log('Hi my name is ' + animal[key]); | |
} else if (key === 'tagline') { | |
console.log('I like to say ' + animal[key]); | |
} | |
} | |
/* | |
* Chapter II | |
* Array exercises | |
*/ | |
var noiseArray = ['meow']; | |
// add item to the beginning | |
noiseArray.unshift('guau'); | |
// add item to the end | |
noiseArray.push('w00t'); | |
/* | |
* Add item to the end using | |
* Brackets notation | |
*/ | |
noiseArray[noiseArray.length] = 'boow'; | |
console.log(noiseArray.length); | |
//console.log(noiseArray); | |
// Add noiseArray to animal object on | |
// noise propertie | |
animal.noises = noiseArray; | |
//console.log(animal); | |
/* | |
* Animal collection | |
*/ | |
var animals = []; | |
// Add the animal object to animals array | |
animals.push(animal); | |
// Assing quackers object and assign it | |
var quackers = { | |
username: 'DaffyDuck', | |
tagline: 'Yippeee!', | |
noises: ['quack', 'honk', 'sneeze', 'growl'] | |
} | |
// Create two more animals and | |
// push it to array | |
var camel = { | |
username: 'Watuki', | |
tagline: 'Heyyya', | |
noises: ['quick', 'cuiiit', 'quack', 'bow'] | |
}; | |
var kitty = { | |
username: 'Kittykat', | |
tagline: 'Meeeowwoowow', | |
noises: ['meeeaow', 'quiiiiick', 'oooar'] | |
}; | |
animals.push(quackers, camel, kitty) | |
console.log('animals obj', animals); | |
// Check length | |
console.log(animals.length); | |
/* | |
* Functions | |
* encapsulation, part of code | |
* runs something | |
* A function is a blender | |
* You can have a factory that makes blender | |
*/ | |
// Function definition | |
// Function body | |
// Create Animal Maker with loop | |
var AnimalMaker = function AnimalMaker(name) { | |
return { | |
speak: function() { | |
console.log('my name is', name); | |
} | |
} | |
} | |
var myAnimal = AnimalMaker('Wootner'); | |
var animalNames = ['Sheep', 'Tiger', 'Big Bird']; | |
for (i = 0; i < animalNames.length; i++) { | |
console.log(animalNames[i]); | |
} | |
/* Function's Exercise | |
* - Write a function AnimalTestUser | |
* - Has one string param (username) | |
* - Returns an object with username prop | |
*/ | |
var AnimalTestUser = function AnimalTestUser(username) { | |
var otherArgs = []; | |
// Check how many args are passed | |
console.log(arguments.length); | |
// If there is more than one arg create a property otherArgs | |
if (arguments.length >= 1) { | |
// otherArgs is an array of remaining arguments | |
for (var i = 1; i < arguments.length; i++) { | |
otherArgs.push(arguments[i]); | |
} | |
// arguments is not a true array, convert it to array like | |
console.log('otherArgs', otherArgs); | |
} | |
return { | |
username: username, | |
otherArgs: otherArgs | |
} | |
} | |
var testSheep = AnimalTestUser('CottonBall', {'loves dancing': true}, [1,2,3]); | |
console.log(testSheep); | |
/* | |
* Write a contruction function AnimalCreator | |
* - Construction Function has 4 params: username, species, tagline and noises | |
* - Animal object should have 5 props: username, species, noises, tagline and friends | |
* - noises and friends are arrays | |
*/ | |
var AnimalCreator = function AnimalCreator(username, species, tagline, noises) { | |
var animal = { | |
username: username, | |
species: species, | |
tagline: tagline, | |
noises: [], | |
friends: [] | |
} | |
if (noises instanceof Array) { | |
for (i=0; i < noises.length; i++) { | |
animal.noises.push(noises[i]); | |
} | |
} | |
else { | |
alert('Noises must be an array!'); | |
} | |
return animal; | |
} | |
var sheep = AnimalCreator('Cloud', 'sheep', 'You can count on me!', ['baahhh', 'arrgg', 'chewchewchew']) | |
console.log('AnimalCreator', sheep); | |
/* | |
* Write a function addFriend | |
* - Takes an animal object | |
* - Adds another animal object as a friend | |
*/ | |
var cow = AnimalCreator('Moo', 'cow', 'I am cow!', ['mooo', 'woo', 'coooow']) | |
console.log('AnimalCreator Cow', cow); | |
var addFriend = function(animal, friend) { | |
animal.friends.push(friend.username); | |
} | |
addFriend(sheep, cow); | |
// Add second friend | |
var llama = AnimalCreator('Zeny', 'llama', 'Whataa llama!', ['cuiii', 'lelz', 'refs']) | |
addFriend(sheep, llama); | |
// Make sure all animals have friends | |
addFriend(cow, llama); | |
addFriend(llama, sheep); | |
console.log('Sheep', sheep); | |
// Create a farm (myFarm) with 3 animal objects | |
var myFarm = [sheep, cow, llama]; | |
console.log('Your farm', myFarm); | |
/* | |
* Create addMatchesArray function | |
* - Takes an array of animal objects | |
* - Adds a new prop to each animal obj called matches | |
*/ | |
var addMatchesArray = function(animals) { | |
for (var i=0; i < animals.length; i++) { | |
animals[i].matches = []; | |
} | |
} | |
addMatchesArray(myFarm); | |
console.log(myFarm); | |
/* | |
* Create a function giveMatches | |
* - Take array of animal objects | |
* - Selects a name from the friends array | |
* - Adds it to the matches array | |
* - Make sure all animal have friends | |
*/ | |
var giveMatches = function(animals) { | |
for (var i = 0; i < animals.lenght; i++) { | |
var selectedAnimal = animals[Math.floor(Math.random() * animals.length)]; | |
console.log('Selected Animal', selectedAnimal); | |
animals[i].matches.push(selectedAnimal.username); | |
} | |
return animals; | |
} | |
console.log(giveMatches(myFarm)); | |
/* | |
* Nesting | |
*/ | |
var box = { | |
innerbox: {} | |
} | |
console.log('box 1', box); | |
var box2 = {} | |
box2['innerbox'] = {} | |
box2['innerbox']['full'] = true; | |
console.log('box 2', box2); | |
// Tiple nesting | |
/* Assign by value or reference | |
* Objects, arrays and functions are by reference | |
* Primitives like string, boolean, undefined by value | |
*/ | |
var box = {}; | |
box['innerBox'] = {}; | |
box['innerBox'].full = true; | |
/* | |
box: { | |
innerBox: { | |
full: true | |
} | |
} | |
*/ | |
box['innerBox']['height'] = 100; | |
// do it here | |
box.innerBox2 = {}; | |
var innerBox2 = 'innerBox2'; | |
box[innerBox2].full = false; | |
console.log('Box', box); | |
/* | |
* ## Exercises for Nesting ## | |
* Represent relationships between two animals in our colection | |
* App has a friendlist on animal's profile whihc list animal's friends | |
* | |
*/ | |
// Create an array for the list of friend's usernames | |
var friends = []; | |
// Using animal array add two usernames to friends | |
friends.push(animals[0].username, animals[1].username); | |
console.log('Friends', friends); | |
// Create a relationship object | |
var relationships = {}; | |
relationships.friends = friends; | |
// What's the length? | |
console.log('Relationships length', Object.keys(relationships).length); // undefined | |
// Create variable matches and assign it to an empty array | |
var matches = []; | |
// Add it to relationship object | |
relationships.matches = matches; | |
// Add at least one username to matches | |
relationships.matches.push(relationships.friends[0]); | |
// Loop animal collection and add relationship obj to each object | |
for (var i = 0; i < animals.length; i++) { | |
animals[i].relationships = relationships; | |
} | |
// Inspect relationship object | |
console.log('Relationships', relationships); | |
// Inspect animals | |
console.log('Animals', animals) | |
/* | |
* Scope: where variable have meaning | |
* - Created dynamically when in function call time | |
* - Everytime you call a function a scope is created | |
*/ | |
// Local Scope | |
var func = function() { | |
var local = true; | |
} | |
// console.log(local); // err! | |
// Global Scope (window object in browser) | |
var x = 'global'; | |
// You can leave var off and make is global | |
function encapsulate() { | |
z = 'this is global, too!'; | |
window.y = 'this is also global!'; | |
} | |
// Parent vs Child Scope | |
// Which scopes have access to which variables ? | |
// The parent CANNOT access the child scope | |
// Where can we access this variables in this nested scope | |
// This is how you create private variables | |
// Scope is not created until you call function | |
var g = 'global'; | |
function blender(fruit){ | |
var b = fruit; | |
var y = 'yogurt'; | |
function bs() { | |
alert( b + ' and ' + y + ' makes ' + b + ' swirl.'); | |
} | |
bs(); | |
} | |
blender('blueberry'); | |
/* | |
* Precedence | |
* Which variable wins if you have two variables with same name | |
*/ | |
var g = 'global'; | |
function go() { | |
var l = 'local'; | |
var g = 'in here!'; | |
console.log(g + ' inside go'); // in here! inside go | |
} | |
go(); | |
alert(g + ' outside go'); //global outside go | |
var inBlock = false; | |
for (var i = 0; i < 5; i++) { | |
var inBlock = true; | |
} | |
if (inBlock) { | |
console.log('Is there block scope? ' + !inBlock); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment