Showing posts with label methods. Show all posts
Showing posts with label methods. Show all posts
Thursday, April 18, 2013
Javascript: Objects in Review
Objects give us a way to represent things--IRL or virtual. We're able to do this by storing information within the object's properties.
So far, we have learned two ways to make objects:
// literal notation, where we use var Name = { };
var cutedork = {
age: 18,
country: "United States"
};
// constructor notation, where we use the keyword new
var cutedork2 = new Object();
cutedork2.age = 18;
cutedork2.country = "United States";
Remember what properties are? They're similar to variables... except that they belong to an object and hold information. We can access properties in two ways:
// dot notation, with ObjectName.PropertyName
var age = jenny.ages
// bracket notation, with ObjectName["PropertyName"] (--don't forget the quotes!)
var country = jenny['country']
Methods act like functions associated with an object and they're especially useful when you ether want to:
1. Update the object properties
2. Calculate something based on an object's properties
// example of defining area and perimeter methods
function Circle (radius) {
this.radius = radius;
this.area = function () {
return Math.PI * this.radius * this.radius;
};
function Perimeter (radius) {
this.radius = radius;
this.area = function() {
return 2 * Math.PI * this.radius;
};
}}
Lastly--in addition to our basic object constructor, we can create our own custom constructors which are helpful for two reasons:
1. We can assign our object properties through parameters we pass in when the object is created.
2. We can give our object methods automatically.
// 3 lines are required to make mdfrmscrtch
var mdfrmscrtch = new Object();
mdfrmscrtch.blogURL = "http://mdrmscrtch.blogspot.com";
mdfrmscrtch.creator = "Jenny"
// A custom constructor for blog
function Blog (blogURL, creator) {
this.blogURL = blogURL;
this.creator = creator;
}
// Use this new creator to make hello_cutedork in one line!
var hello_cutedork = new Blog("http://hellocutedork.blogspot.com", "Jenny");
Song of the day // Lucky Ones (Lana Del Rey)
Labels:
bracket,
codeacademy,
coding,
constructors,
CS,
dot,
javascript,
literal,
methods,
notation,
objects,
programming,
properties,
review
Javascript: Methods
![]() |
| my brother's science project |
Methods are an important part of OOP (object oriented programming). Remember last time we worked with properties? Whereas properties are similar to variables, a method is like a function associated with an object. Methods are important because they serve several purposes including:
1. The ability to change object property values.
2. The ability to make calculations based on object properties. Functions can only use parameters as an input, but methods can make calculations with object properties. Cool, huh?
Here's an example:
// make cutedork here, and first give her an age of 8
var cutedork = new Object();
cutedork.age = 8;
cutedork.setAge = setAge;
// here, update cutedork's age to 18 using the method
cutedork.setAge(18);
Another slightly more challenging example:
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();
Labels:
codeacademy,
coding,
CS,
javascript,
JS,
methods,
programming
Subscribe to:
Posts (Atom)

