SlideShare a Scribd company logo
BEGINNING JAVASCRIPT
CLASS 2Javascript ~ Girl Develop It ~
GDI Seattle - Intro to JavaScript Class 2
WELCOME!
Girl Develop It is here to provide affordable and
accessible programs to learn software through
mentorship and hands-on instruction.
Some "rules"
We are here for you!
Every question is important
Help each other
Have fun
ARRAY
An array is a data-type that holds an ordered list
of values, of any type:
The length property reports the size of the array:
var arrayName = [element0, element1, ...];
var rainbowColors = ['Red', 'Orange', 'Yellow', 'Green', 'Blue', 'Indigo', 'Violet'];
var favoriteNumbers = [16, 27, 88];
var luckyThings = ['Rainbows', 7, 'Horseshoes'];
console.log(rainbowColors.length);
ARRAYS -- RETURNING
VALUES
You can access items with "bracket notation".
The number inside the brackets is called an
"index"
Arrays in JavaScript are "zero-indexed", which
means we start counting from zero.
var arrayItem = arrayName[indexNum];
var rainbowColors = ['Red', 'Orange', 'Yellow', 'Green', 'Blue', 'Indigo', 'Violet'];
var firstColor = rainbowColors[0];
var lastColor = rainbowColors[6];
ARRAYS -- UPDATING
VALUES
You can also use bracket notation to change the
item in an array:
Or to add to an array:
You can also use the push method
(recommended):
var awesomeAnimals = ['Corgis', 'Otters', 'Octopi'];
awesomeAnimals[0] = 'Bunnies';
awesomeAnimals[4] = 'Corgis';
awesomeAnimals.push('Ocelots');
LOOPS
Sometimes you want to go through a piece of
code multiple times
Why?
Showing a timer count down.
Display the results of a search.
Sort a list of values.
THE WHILE LOOP
The while loop tells JS to repeat statements while
a condition is true:
Review: '++' means increment by 1!
If we forget x++, the loop will never end!
while (expression) {
// statements to repeat
}
var x = 0;
while (x < 5) {
console.log(x);
x++;
}
THE FOR LOOP
The for loop is a safer way of looping
Less danger of an infinite loop. All conditions are
at the top!
for (initialize; condition; update) {
// statements to repeat
}
for (var i = 0; i < 5; i++) {
console.log(i);
}
LOOPS AND ARRAYS
Use a for loop to easily look at each item in an
array:
var rainbowColors = ['Red', 'Orange', 'Yellow', 'Green', 'Blue', 'Indigo', 'Violet'];
for (var i = 0; i < rainbowColors.length; i++) {
console.log(rainbowColors[i]);
}
LET'S DEVELOP IT
Add a new button to the exercise from last
week.
Add an onclick to the button for a function
called favoriteThings().
Create a new function called favoriteThings() in
the JavaScript file.
In the function, create an array and loop
through the results.
Write the results to the document like: "My
favorite things are: XX, YY, ZZ"
Bonus -- add an 'and' in the sentence before
the last item
LET'S ANSWER IT<button type="button" onclick="favoriteThings()"> See my favorite things</button>
function favoriteThings(){
var favoriteThings = ['Rabbits', 'Orange', 'Yogurt', 'Brussel Sprouts', 'Otters'];
var result = 'My favorite things are: ';
for (var i = 0; i < favoriteThings.length; i++) {
result += favoriteThings[i] + ', ';
}
document.write(result);
}
LET'S ANSWER IT (BONUS)function favoriteThings(){
var favoriteThings = ['Rabbits', 'Orange', 'Yogurt', 'Brussel Sprouts', 'Otters'];
var result = 'My favorite things are: ';
for (var i = 0; i < favoriteThings.length; i++) {
if (i < favoriteThings.length - 1) {
result += favoriteThings[i] + ', ';
}
else {
result += "and " + favoriteThings[i] + '.';
}
}
document.write(result);
}
OBJECTS
Objects are a data type that let us store a
collection of properties and methods.
var objectName = {
propertyName: propertyValue,
propertyName: propertyValue,
...
};
var charlie = {
age: 8,
name: "Charlie Brown",
likes: ["baseball", "The little red-haired girl"],
pet: "Snoopy"
};
OBJECTS -- RETURNING
VALUES
Access values of "properties" using "dot
notation":
var charlie = {
age: 8,
name: "Charlie Brown",
likes: ["baseball", "The little red-haired girl"],
pet: "Snoopy"
};
var pet = charlie.pet;
OBJECTS -- RETURNING
VALUES
Or using "bracket notation" (like arrays):
Non-existent properties will return undefined:
var name = charlie['name'];
var gender = charlie.gender
OBJECTS -- CHANGING
VALUES
Use dot or bracket notation with the assignment
operator to change objects.
Change existing properties:
Or add new properties:
You can also delete properties:
charlie.name = "Chuck";
charlie.gender = "male";
delete charlie.gender;
ARRAYS OF OBJECTS
Arrays can hold objects too!
That means we can use a for loop!
var peanuts = [
{name: "Charlie Brown",
pet: "Snoopy"},
{name: "Linus van Pelt",
pet: "Blue Blanket"}
];
for (var i = 0; i < peanuts.length; i++) {
var peanut = peanuts[i];
console.log(peanut.name + ' has a pet named ' + peanut.pet + '.');
}
OBJECTS IN FUNCTIONS
You can pass an object into a function as a
parameter
var peanut ={
name: "Charlie Brown",
pet: "Snoopy"
};
function describeCharacter(character){
console.log(character.name + ' has a pet named ' + character.pet + '.');
}
describeCharacter(peanut);
METHODS
Methods are functions that are associated with
an object
The affect or return a value for a specific object
Used with dot notation
Previously seen example:
document.write("Hello, world!");
METHODS
Methods can be added to objects in two ways.
Declared with the object.
Attached using dot notation.
var charlie = {
name: "Charlie",
sayHello: function () {
document.write("My name is Charlie.");
}
};
charlie.sayHello();
var charlie = { name: "Charlie"};
function sayName() {
document.write("My name is Charlie.");
}
charlie.sayHello = sayName;
charlie.sayHello();
THIS
Inside methods, properties are accessed using
the this keyword.
this refers to the "owner" of the property.
var charlie = {
name: "Charlie",
sayHello: function () {
document.write("My name is " + this.name + ".");
}
};
var lucy = {
name: "Lucy van Pelt",
sayHello: function () {
document.write("My name is " + this.name + ".");
}
};
charlie.sayHello(); //My name is Charlie.
lucy.sayHello(); //My name is Lucy van Pelt.
LET'S DEVELOP IT
Add another button that calls the function
myFriends() on click.
Add a new function called myFriends to the
JavaScript.
In the function, create an array of friends
objects, with their names and hair colors.
Use a for loop to go through each friend and
describe them.
Write the results to the document.
Bonus -- make a separate functions that
describe the friends
LET'S ANSWER IT<button href = "#" onclick="myFriends()">My friends</button>
function myFriends(){
var friends = [
{name: 'Santa Claus',
hair: 'red'},
{name: 'Easter Bunny',
hair: 'brown'},
{name: 'Tooth Fairy',
hair: 'blue'}
];
var results = "My friends: "
for(var i = 0; i < friends.length; i++){
document.write("My friend " + friend[i].name + " has " + friend[i].hair + " hair. ");
}
}
LET'S ANSWER IT (BONUS)function myFriends(){
var friends = [
{name: 'Santa Claus',
hair: 'red'},
{name: 'Easter Bunny',
hair: 'brown'},
{name: 'Tooth Fairy',
hair: 'blue'}
];
var results = "My friends: "
for(var i = 0; i < friends.length; i++){
results += describeFriend(friends[i]);
}
document.write(results)
}
function describeFriend(friend){
return "My friend " + friend.name + " has " + friend.hair + " hair. ";
}
QUESTIONS?
?
GDI Seattle - Intro to JavaScript Class 2

More Related Content

PDF
PHP Unit 4 arrays
Kumar
 
PDF
Php array
Nikul Shah
 
PDF
4.1 PHP Arrays
Jalpesh Vasa
 
PDF
Arrays in PHP
Vineet Kumar Saini
 
PDF
DBIx::Class beginners
leo lapworth
 
PPTX
PHP Functions & Arrays
Henry Osborne
 
PPT
Class 4 - PHP Arrays
Ahmed Swilam
 
PDF
DBIx::Class introduction - 2010
leo lapworth
 
PHP Unit 4 arrays
Kumar
 
Php array
Nikul Shah
 
4.1 PHP Arrays
Jalpesh Vasa
 
Arrays in PHP
Vineet Kumar Saini
 
DBIx::Class beginners
leo lapworth
 
PHP Functions & Arrays
Henry Osborne
 
Class 4 - PHP Arrays
Ahmed Swilam
 
DBIx::Class introduction - 2010
leo lapworth
 

What's hot (18)

PDF
Scripting3
Nao Dara
 
PPTX
Chap 3php array part1
monikadeshmane
 
PDF
Handout - Introduction to Programming
Cindy Royal
 
PPT
Php array
Core Lee
 
PDF
How to write code you won't hate tomorrow
Pete McFarlane
 
PPT
Php Using Arrays
mussawir20
 
PPT
MySQLConf2009: Taking ActiveRecord to the Next Level
Blythe Dunham
 
PPT
Arrays in PHP
Compare Infobase Limited
 
PPTX
PHP array 1
Mudasir Syed
 
PDF
Chaining and function composition with lodash / underscore
Nicolas Carlo
 
PDF
Lodash js
LearningTech
 
PPTX
Java script arrays
Frayosh Wadia
 
PDF
Your code sucks, let's fix it
Rafael Dohms
 
PPTX
Marcs (bio)perl course
BITS
 
PDF
Python - Lecture 3
Ravi Kiran Khareedi
 
PPTX
Ch8(oop)
Chhom Karath
 
PDF
Marc’s (bio)perl course
Marc Logghe
 
Scripting3
Nao Dara
 
Chap 3php array part1
monikadeshmane
 
Handout - Introduction to Programming
Cindy Royal
 
Php array
Core Lee
 
How to write code you won't hate tomorrow
Pete McFarlane
 
Php Using Arrays
mussawir20
 
MySQLConf2009: Taking ActiveRecord to the Next Level
Blythe Dunham
 
PHP array 1
Mudasir Syed
 
Chaining and function composition with lodash / underscore
Nicolas Carlo
 
Lodash js
LearningTech
 
Java script arrays
Frayosh Wadia
 
Your code sucks, let's fix it
Rafael Dohms
 
Marcs (bio)perl course
BITS
 
Python - Lecture 3
Ravi Kiran Khareedi
 
Ch8(oop)
Chhom Karath
 
Marc’s (bio)perl course
Marc Logghe
 
Ad

Viewers also liked (8)

PDF
New cervical screening guidelines. 1
CSPWQ
 
PDF
Kuala Lumpur Hotels
quicksweet
 
PDF
GDI Seattle - Intermediate HTML and CSS Class 3 Slides
Heather Rock
 
PDF
GDI Seattle - Intro to JavaScript Class 1
Heather Rock
 
PPTX
Pic pas demo
Arsen Lukpanov
 
PPTX
Nelson mandela
gauravamity
 
PDF
GDI Seattle - Web Accessibility Class 1
Heather Rock
 
PPSX
Prova origami
piranzor
 
New cervical screening guidelines. 1
CSPWQ
 
Kuala Lumpur Hotels
quicksweet
 
GDI Seattle - Intermediate HTML and CSS Class 3 Slides
Heather Rock
 
GDI Seattle - Intro to JavaScript Class 1
Heather Rock
 
Pic pas demo
Arsen Lukpanov
 
Nelson mandela
gauravamity
 
GDI Seattle - Web Accessibility Class 1
Heather Rock
 
Prova origami
piranzor
 
Ad

Similar to GDI Seattle - Intro to JavaScript Class 2 (20)

PPTX
Javascript 101
Shlomi Komemi
 
PDF
JAVASCRIPT OBJECTS.pdf
cherop41618145
 
PPTX
The JavaScript Programming Language
Mohammed Irfan Shaikh
 
PDF
Fewd week5 slides
William Myers
 
PPTX
CSC PPT 13.pptx
DrRavneetSingh
 
PDF
Stuff you didn't know about action script
Christophe Herreman
 
PPTX
OOP in PHP.pptx
switipatel4
 
PPT
JavaScript Tutorial
Bui Kiet
 
PDF
Intro to Advanced JavaScript
ryanstout
 
PDF
Powerful JavaScript Tips and Best Practices
Dragos Ionita
 
PPTX
Php & my sql
Norhisyam Dasuki
 
PDF
The Future of JavaScript (SXSW '07)
Aaron Gustafson
 
PPTX
Object oriented javascript
Shah Jalal
 
PDF
05 JavaScript #burningkeyboards
Denis Ristic
 
PPTX
Js types
LearningTech
 
PPTX
Chap1introppt2php(finally done)
monikadeshmane
 
PPTX
The ES Library for JavaScript Developers
Ganesh Bhosale
 
PPT
Artdm170 Week10 Arrays Math
Gilbert Guerrero
 
PPT
ARTDM 170, Week10: Arrays + Using Randomization
Gilbert Guerrero
 
PDF
I, For One, Welcome Our New Perl6 Overlords
heumann
 
Javascript 101
Shlomi Komemi
 
JAVASCRIPT OBJECTS.pdf
cherop41618145
 
The JavaScript Programming Language
Mohammed Irfan Shaikh
 
Fewd week5 slides
William Myers
 
CSC PPT 13.pptx
DrRavneetSingh
 
Stuff you didn't know about action script
Christophe Herreman
 
OOP in PHP.pptx
switipatel4
 
JavaScript Tutorial
Bui Kiet
 
Intro to Advanced JavaScript
ryanstout
 
Powerful JavaScript Tips and Best Practices
Dragos Ionita
 
Php & my sql
Norhisyam Dasuki
 
The Future of JavaScript (SXSW '07)
Aaron Gustafson
 
Object oriented javascript
Shah Jalal
 
05 JavaScript #burningkeyboards
Denis Ristic
 
Js types
LearningTech
 
Chap1introppt2php(finally done)
monikadeshmane
 
The ES Library for JavaScript Developers
Ganesh Bhosale
 
Artdm170 Week10 Arrays Math
Gilbert Guerrero
 
ARTDM 170, Week10: Arrays + Using Randomization
Gilbert Guerrero
 
I, For One, Welcome Our New Perl6 Overlords
heumann
 

More from Heather Rock (6)

PDF
GDI Seattle - Intro to JavaScript Class 4
Heather Rock
 
PDF
GDI Seattle Intro to HTML and CSS - Class 4
Heather Rock
 
PDF
GDI Seattle Intro to HTML and CSS - Class 3
Heather Rock
 
PDF
Intro to HTML and CSS - Class 2 Slides
Heather Rock
 
PDF
GDI Seattle Intermediate HTML and CSS Class 1
Heather Rock
 
PDF
GDI Seattle Intro to HTML and CSS - Class 1
Heather Rock
 
GDI Seattle - Intro to JavaScript Class 4
Heather Rock
 
GDI Seattle Intro to HTML and CSS - Class 4
Heather Rock
 
GDI Seattle Intro to HTML and CSS - Class 3
Heather Rock
 
Intro to HTML and CSS - Class 2 Slides
Heather Rock
 
GDI Seattle Intermediate HTML and CSS Class 1
Heather Rock
 
GDI Seattle Intro to HTML and CSS - Class 1
Heather Rock
 

Recently uploaded (20)

PDF
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
PPTX
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
PDF
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
PDF
How-Cloud-Computing-Impacts-Businesses-in-2025-and-Beyond.pdf
Artjoker Software Development Company
 
PDF
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
PPTX
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
PDF
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
PPTX
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
PDF
Software Development Company | KodekX
KodekX
 
PDF
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
PPTX
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
PPTX
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
PDF
Architecture of the Future (09152021)
EdwardMeyman
 
PDF
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
PDF
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
PDF
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
codernjn73
 
PDF
Brief History of Internet - Early Days of Internet
sutharharshit158
 
PDF
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
PDF
A Day in the Life of Location Data - Turning Where into How.pdf
Precisely
 
PDF
This slide provides an overview Technology
mineshkharadi333
 
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
How-Cloud-Computing-Impacts-Businesses-in-2025-and-Beyond.pdf
Artjoker Software Development Company
 
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
Software Development Company | KodekX
KodekX
 
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
Architecture of the Future (09152021)
EdwardMeyman
 
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
codernjn73
 
Brief History of Internet - Early Days of Internet
sutharharshit158
 
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
A Day in the Life of Location Data - Turning Where into How.pdf
Precisely
 
This slide provides an overview Technology
mineshkharadi333
 

GDI Seattle - Intro to JavaScript Class 2

  • 3. WELCOME! Girl Develop It is here to provide affordable and accessible programs to learn software through mentorship and hands-on instruction. Some "rules" We are here for you! Every question is important Help each other Have fun
  • 4. ARRAY An array is a data-type that holds an ordered list of values, of any type: The length property reports the size of the array: var arrayName = [element0, element1, ...]; var rainbowColors = ['Red', 'Orange', 'Yellow', 'Green', 'Blue', 'Indigo', 'Violet']; var favoriteNumbers = [16, 27, 88]; var luckyThings = ['Rainbows', 7, 'Horseshoes']; console.log(rainbowColors.length);
  • 5. ARRAYS -- RETURNING VALUES You can access items with "bracket notation". The number inside the brackets is called an "index" Arrays in JavaScript are "zero-indexed", which means we start counting from zero. var arrayItem = arrayName[indexNum]; var rainbowColors = ['Red', 'Orange', 'Yellow', 'Green', 'Blue', 'Indigo', 'Violet']; var firstColor = rainbowColors[0]; var lastColor = rainbowColors[6];
  • 6. ARRAYS -- UPDATING VALUES You can also use bracket notation to change the item in an array: Or to add to an array: You can also use the push method (recommended): var awesomeAnimals = ['Corgis', 'Otters', 'Octopi']; awesomeAnimals[0] = 'Bunnies'; awesomeAnimals[4] = 'Corgis'; awesomeAnimals.push('Ocelots');
  • 7. LOOPS Sometimes you want to go through a piece of code multiple times Why? Showing a timer count down. Display the results of a search. Sort a list of values.
  • 8. THE WHILE LOOP The while loop tells JS to repeat statements while a condition is true: Review: '++' means increment by 1! If we forget x++, the loop will never end! while (expression) { // statements to repeat } var x = 0; while (x < 5) { console.log(x); x++; }
  • 9. THE FOR LOOP The for loop is a safer way of looping Less danger of an infinite loop. All conditions are at the top! for (initialize; condition; update) { // statements to repeat } for (var i = 0; i < 5; i++) { console.log(i); }
  • 10. LOOPS AND ARRAYS Use a for loop to easily look at each item in an array: var rainbowColors = ['Red', 'Orange', 'Yellow', 'Green', 'Blue', 'Indigo', 'Violet']; for (var i = 0; i < rainbowColors.length; i++) { console.log(rainbowColors[i]); }
  • 11. LET'S DEVELOP IT Add a new button to the exercise from last week. Add an onclick to the button for a function called favoriteThings(). Create a new function called favoriteThings() in the JavaScript file. In the function, create an array and loop through the results. Write the results to the document like: "My favorite things are: XX, YY, ZZ" Bonus -- add an 'and' in the sentence before the last item
  • 12. LET'S ANSWER IT<button type="button" onclick="favoriteThings()"> See my favorite things</button> function favoriteThings(){ var favoriteThings = ['Rabbits', 'Orange', 'Yogurt', 'Brussel Sprouts', 'Otters']; var result = 'My favorite things are: '; for (var i = 0; i < favoriteThings.length; i++) { result += favoriteThings[i] + ', '; } document.write(result); }
  • 13. LET'S ANSWER IT (BONUS)function favoriteThings(){ var favoriteThings = ['Rabbits', 'Orange', 'Yogurt', 'Brussel Sprouts', 'Otters']; var result = 'My favorite things are: '; for (var i = 0; i < favoriteThings.length; i++) { if (i < favoriteThings.length - 1) { result += favoriteThings[i] + ', '; } else { result += "and " + favoriteThings[i] + '.'; } } document.write(result); }
  • 14. OBJECTS Objects are a data type that let us store a collection of properties and methods. var objectName = { propertyName: propertyValue, propertyName: propertyValue, ... }; var charlie = { age: 8, name: "Charlie Brown", likes: ["baseball", "The little red-haired girl"], pet: "Snoopy" };
  • 15. OBJECTS -- RETURNING VALUES Access values of "properties" using "dot notation": var charlie = { age: 8, name: "Charlie Brown", likes: ["baseball", "The little red-haired girl"], pet: "Snoopy" }; var pet = charlie.pet;
  • 16. OBJECTS -- RETURNING VALUES Or using "bracket notation" (like arrays): Non-existent properties will return undefined: var name = charlie['name']; var gender = charlie.gender
  • 17. OBJECTS -- CHANGING VALUES Use dot or bracket notation with the assignment operator to change objects. Change existing properties: Or add new properties: You can also delete properties: charlie.name = "Chuck"; charlie.gender = "male"; delete charlie.gender;
  • 18. ARRAYS OF OBJECTS Arrays can hold objects too! That means we can use a for loop! var peanuts = [ {name: "Charlie Brown", pet: "Snoopy"}, {name: "Linus van Pelt", pet: "Blue Blanket"} ]; for (var i = 0; i < peanuts.length; i++) { var peanut = peanuts[i]; console.log(peanut.name + ' has a pet named ' + peanut.pet + '.'); }
  • 19. OBJECTS IN FUNCTIONS You can pass an object into a function as a parameter var peanut ={ name: "Charlie Brown", pet: "Snoopy" }; function describeCharacter(character){ console.log(character.name + ' has a pet named ' + character.pet + '.'); } describeCharacter(peanut);
  • 20. METHODS Methods are functions that are associated with an object The affect or return a value for a specific object Used with dot notation Previously seen example: document.write("Hello, world!");
  • 21. METHODS Methods can be added to objects in two ways. Declared with the object. Attached using dot notation. var charlie = { name: "Charlie", sayHello: function () { document.write("My name is Charlie."); } }; charlie.sayHello(); var charlie = { name: "Charlie"}; function sayName() { document.write("My name is Charlie."); } charlie.sayHello = sayName; charlie.sayHello();
  • 22. THIS Inside methods, properties are accessed using the this keyword. this refers to the "owner" of the property. var charlie = { name: "Charlie", sayHello: function () { document.write("My name is " + this.name + "."); } }; var lucy = { name: "Lucy van Pelt", sayHello: function () { document.write("My name is " + this.name + "."); } }; charlie.sayHello(); //My name is Charlie. lucy.sayHello(); //My name is Lucy van Pelt.
  • 23. LET'S DEVELOP IT Add another button that calls the function myFriends() on click. Add a new function called myFriends to the JavaScript. In the function, create an array of friends objects, with their names and hair colors. Use a for loop to go through each friend and describe them. Write the results to the document. Bonus -- make a separate functions that describe the friends
  • 24. LET'S ANSWER IT<button href = "#" onclick="myFriends()">My friends</button> function myFriends(){ var friends = [ {name: 'Santa Claus', hair: 'red'}, {name: 'Easter Bunny', hair: 'brown'}, {name: 'Tooth Fairy', hair: 'blue'} ]; var results = "My friends: " for(var i = 0; i < friends.length; i++){ document.write("My friend " + friend[i].name + " has " + friend[i].hair + " hair. "); } }
  • 25. LET'S ANSWER IT (BONUS)function myFriends(){ var friends = [ {name: 'Santa Claus', hair: 'red'}, {name: 'Easter Bunny', hair: 'brown'}, {name: 'Tooth Fairy', hair: 'blue'} ]; var results = "My friends: " for(var i = 0; i < friends.length; i++){ results += describeFriend(friends[i]); } document.write(results) } function describeFriend(friend){ return "My friend " + friend.name + " has " + friend.hair + " hair. "; }