Week 5は面白かった。Constructorメソッドやら、オブジェクトリテラルやら。
Week 6も結構楽しみ。
This file contains 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
// 1. We've come a long, long... | |
var answer = true; | |
// 2. Through the hard times... | |
// Here is an array of multiples of 8. But is it correct? | |
var multiplesOfEight = [8,16,24,32,40,58]; | |
// Test to see if a number from the array is NOT a true | |
// multiple of eight. Real multiples will return false. | |
var answer = multiplesOfEight[5] % 8 !== 0; | |
// 3. ...And the good! | |
for(var n = 0; n < 20; n++){ | |
var m = n + 1; | |
if(0 === m % 3){ | |
if(0 === m % 5) { | |
console.log("FizzBuzz"); | |
}else{ | |
console.log("Fizz"); | |
} | |
}else if(0 === m % 5){ | |
console.log("Buzz"); | |
}else{ | |
console.log(m); | |
} | |
} | |
// 4. I have to celebrate you baby | |
var getReview = function (movie) { | |
switch(movie){ | |
case "Matrix": | |
return "good trip out"; | |
case "Princess Bride": | |
return "awesome date night movie"; | |
case "Welcome to America": | |
return "Amjad's favorite"; | |
case "Remember the Titans": | |
return "love the sports"; | |
case "Why do I look like I'm 12?": | |
return "The Ryan and Zach story"; | |
case "Fighting Kangaroos in the wild": | |
return "Token Australian movie for Leng"; | |
default: | |
return "I don't know!"; | |
} | |
}; | |
// 5. I have to praise you like I should! | |
// any topics you want us to review, email contact@codecademy.com | |
// if you want to see the winning joke for this week, see the hint! | |
// if you think you have a better joke, send it in! | |
console.log("I'm ready for Objects!"); |
This file contains 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
// 1. Intro | |
var bob = {}; | |
// 2. Properties | |
var Spencer = { | |
age: 22, | |
country: "United States" | |
}; | |
// make your own object here called Me | |
var Me = { | |
age: 29, | |
country: "Japan" | |
}; | |
// 3. Accessing Properties | |
var bob = { | |
name: "Bob Smith", | |
age: 30 | |
}; | |
var susan = { | |
name: "Susan Jordan", | |
age: 25 | |
}; | |
// here we save Bob's information | |
var name1 = bob.name; | |
var age1 = bob.age; | |
// finish this code by saving Susan's information | |
var name2 = susan.name; | |
var age2 = susan.age; | |
// 4. Accessing Properties, Part 2 | |
// Take a look at our next example object, a dog | |
var dog = { | |
species: "greyhound", | |
weight: 60, | |
age: 4 | |
}; | |
var species = dog["species"]; | |
// fill in the code to save the weight and age using bracket notation | |
var weight = dog["weight"]; | |
var age = dog["age"]; | |
// 5. Another Way to Create | |
// Our bob object again, but made using a constructor this time | |
var bob = new Object(); | |
bob.name = "Bob Smith"; | |
bob.age = 30; | |
// Here is susan1, in literal notation | |
var susan1 = { | |
name: "Susan Jordan", | |
age: 24 | |
}; | |
// Make a new susan2 object, using a constructor instead | |
var susan2 = new Object(); | |
susan2.name = susan1.name; | |
susan2.age = susan1.age; | |
// 6. Putting it all together | |
// help us make snoopy using literal notation | |
// Remember snoopy is a "beagle" and is 10 years old. | |
var snoopy = { | |
species: "beagle", | |
age: 10 | |
}; | |
// help make buddy using constructor notation | |
// buddy is a "golden retriever" and is 5 years old | |
var buddy = new Object(); | |
buddy.species = "golden retriever"; | |
buddy.age = 5; | |
// 7. More Practice Making Objects | |
var BMW = { | |
cost: "too much", | |
speed: 220, | |
country: "Germany" | |
}; |
This file contains 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
// 1. Function Review | |
// Accepts a number x as input and returns its square | |
var square = function (x) { | |
return x * x; | |
}; | |
// Write the function multiply below | |
// It should take two parameters and return the product | |
var multiply = function(x, y){ | |
return x * y; | |
}; | |
multiply(1, 1); | |
// 2. So What's a Method? | |
// here is bob again, with his usual properties | |
var bob = new Object(); | |
bob.name = "Bob Smith"; | |
bob.age = 30; | |
// this time we have added a method, setAge | |
bob.setAge = function (newAge){ | |
bob.age = newAge; | |
}; | |
// here we set bob's age to 40 | |
bob.setAge(40); | |
// bob's feeling old. Use our method to set bob's age to 20 | |
bob.setAge(20); | |
// 3. Why Are Methods Important? | |
var bob = new Object(); | |
bob.age = 30; | |
// this time we have added a method, setAge | |
bob.setAge = function (newAge){ | |
bob.age = newAge; | |
}; | |
bob.getYearOfBirth = function () { | |
return 2012 - bob.age; | |
}; | |
console.log(bob.getYearOfBirth()); | |
// 4. The "This" Keyword | |
// here we define our method using "this", before we even introduce bob | |
var setAge = function (newAge) { | |
this.age = newAge; | |
}; | |
// now we make bob | |
var bob = new Object(); | |
bob.age = 30; | |
// and down here we just use the method we already made | |
bob.setAge = setAge; | |
// change bob's age to 50 here | |
bob.setAge(50); | |
// 5. "This" Works for Everyone | |
// here we define our method using "this", before we even introduce bob | |
var setAge = function (newAge) { | |
this.age = newAge; | |
}; | |
// now we make bob | |
var bob = new Object(); | |
bob.age = 30; | |
bob.setAge = setAge; | |
// make susan here, and first give her an age of 25 | |
var susan = new Object(); | |
susan.age = 25; | |
// here, update Susan's age to 35 using the method | |
susan.setAge = setAge; | |
susan.setAge(35); | |
// 6. Make Your Own Method | |
var rectangle = new Object(); | |
rectangle.length = 3; | |
rectangle.width = 4; | |
// here is our method to set the length | |
rectangle.setLength = function (newLength) { | |
this.length = newLength; | |
}; | |
// help by finishing this method | |
rectangle.setWidth = function(newWidth){ | |
this.width = newWidth; | |
}; | |
// here change the width to 8 and length to 6 using our new methods | |
rectangle.setWidth(8); | |
rectangle.setLength(6); | |
// 7. More Kinds of Methods | |
var square = new Object(); | |
square.sideLength = 6; | |
square.calcPerimeter = function() { | |
return this.sideLength * 4; | |
}; | |
// help us define an area method here | |
square.calcArea = function() { | |
return this.sideLength * this.sideLength; | |
}; | |
var p = square.calcPerimeter(); | |
var a = square.calcArea(); |
This file contains 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
// 1. The Object Constructor | |
// here we make bob using the Object constructor | |
var bob = new Object(); | |
bob.name = "Bob Smith"; | |
// add bob's age here and set it equal to 20 | |
bob.age = 20; | |
// 2. Custom Constructors | |
function Person(name,age) { | |
this.name = name; | |
this.age = age; | |
} | |
// Let's make bob and susan again, using our constructor | |
var bob = new Person("Bob Smith", 30); | |
var susan = new Person("Susan Jordan", 25); | |
// help us make george, whose name is "George Washington" and age is 275 | |
var george = new Person("George Washington", 275); | |
// 3. Try it Out! | |
function Cat(age, color) { | |
this.age = age; | |
this.color = color; | |
} | |
// make a Dog constructor here | |
function Dog(name, age) { | |
this.name = name; | |
this.age = age; | |
} | |
// 4. More Options | |
function Person(name,age) { | |
this.name = name; | |
this.age = age; | |
this.species = "Homo Sapiens"; | |
} | |
var sally = new Person("Sally Bowles", 39); | |
var holden = new Person("Holden Coulfield", 16); | |
console.log("sally's species is " + sally.species + " and she is " + sally.age); | |
console.log("holden's species is " + holden.species + " and he is " + holden.age); | |
// 5. Constructors With Methods | |
function Rectangle(length, width) { | |
this.length = length; | |
this.width = width; | |
this.calcArea = function() { | |
return this.length * this.width; | |
}; | |
// put our perimeter function here! | |
this.calcPerimeter = function() { | |
return (this.length + this.width) * 2; | |
} | |
} | |
var rex = new Rectangle(7,3); | |
var area = rex.calcArea(); | |
var perimeter = rex.calcPerimeter(); | |
// 6. Constructors in Review | |
// first we can make the instructor | |
function Rabbit(adjective) { | |
this.adjective = adjective; | |
this.describeMyself = function() { | |
console.log("I am a " + this.adjective + " rabbit"); | |
}; | |
} | |
// now we can easily make all of our rabbits | |
var rabbit1 = new Rabbit("fluffy"); | |
var rabbit2 = new Rabbit("happy"); | |
var rabbit3 = new Rabbit("sleepy"); | |
rabbit1.describeMyself(); | |
rabbit2.describeMyself(); | |
rabbit3.describeMyself(); |
This file contains 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
// 1. Arrays of Objects | |
// Our person constructor | |
function Person (name, age) { | |
this.name = name; | |
this.age = age; | |
} | |
// Now we can make an array of people | |
var family = new Array(); | |
family[0] = new Person("alice", 40); | |
family[1] = new Person("bob", 42); | |
family[2] = new Person("michelle", 8); | |
// add the last family member, "timmy", who is 6 years old | |
family[3] = new Person("timmy", 6); | |
// 2. Loop the loop | |
// Our Person constructor | |
function Person(name, age){ | |
this.name = name; | |
this.age = age; | |
} | |
// Now we can make an array of people | |
var family = new Array(); | |
family[0] = new Person("alice", 40); | |
family[1] = new Person("bob", 42); | |
family[2] = new Person("michelle", 8); | |
family[3] = new Person("timmy", 6); | |
// loop through our new array | |
for(var i = 0; i < 4; i++){ | |
console.log(family[i].name); | |
} | |
// 3. Passing Objects into Functions | |
// Our person constructor | |
function Person (name, age) { | |
this.name = name; | |
this.age = age; | |
} | |
// We can make a function which takes persons as arguments | |
// This one computes the difference in ages between two people | |
var ageDifference = function(person1, person2) { | |
return person1.age - person2.age; | |
} | |
var alice = new Person("Alice", 30); | |
var billy = new Person("Billy", 25); | |
// 4. Try it Out! | |
// Our person constructor | |
function Person (name, age) { | |
this.name = name; | |
this.age = age; | |
} | |
// We can make a function which takes persons as arguments | |
// This one computes the difference in ages between two people | |
var ageDifference = function(person1, person2) { | |
return person1.age - person2.age; | |
}; | |
// Make a new function, olderAge, to return the age of | |
// the older of two people | |
var olderAge = function(person1, person2){ | |
return person1.age < person2.age ? person2.age: person1.age; | |
}; | |
// Let's bring back alice and billy to test our new function | |
var alice = new Person("Alice", 30); | |
var billy = new Person("Billy", 25); | |
console.log("The older person is "+olderAge(alice, billy)); |
This file contains 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
// 1. What Are Objects For? | |
var spencer = { | |
age: 22, | |
country: "United States" | |
}; | |
// make spencer2 here with constructor notation | |
var spencer2 = new Object(); | |
spencer2.age = 22; | |
spencer2.country = "United States"; | |
// 2. Properties | |
var snoopy = new Object(); | |
snoopy.species = "beagle"; | |
snoopy.age = 10; | |
// save Snoopy's age and species into variables | |
// use dot notation for snoopy's species | |
var species = snoopy['species']; | |
// use bracket notation for snoopy's age | |
var age = snoopy['age']; | |
// 3. Methods | |
function Circle (radius) { | |
this.radius = radius; | |
this.area = function () { | |
return Math.PI * this.radius * this.radius; | |
}; | |
// define a perimeter method here | |
this.perimeter = function() { | |
return this.radius * 2 * Math.PI; | |
}; | |
}; | |
// 4. Customizing Constructors | |
// 3 lines required to make the iliad | |
var harry_potter = new Object(); | |
harry_potter.length = 350; | |
harry_potter.author = "J.K. Rowling"; | |
// A custom constructor for book | |
function Book (length, author) { | |
this.length = length; | |
this.author = author; | |
} | |
// Use our new constructor to make the_hobbit in one line | |
var the_hobbit = new Book(320, "J.R.R. Tolkien"); |
0 件のコメント:
コメントを投稿