So let's refresh.
Properties are simply variables attached to an object. Here's an example of an object and property pair:
plan2.name
plan2 is the object, while name is the property. Here's a straightforward way to define an object and its properties:
var plan1 = {
name: "Basic",
price: 3.99,
space: 100,
transfer: 1000,
pages: 10
};
plan1 is the object name. The properties within are in this format, "name: value" with a comma after each value, except for the last.
Here we are adding a property with an array as its value
plan1.discountMonths = [6, 7, 11];
So that would mean that July, August, and December are the discount months (remember that JavaScript assigns 0 through 11 for the months of the year). This would refer to the "11" in the array:
plan1.discountMonths[2];
That's the object name, the property, and the index of the desired value in the array. You could change the value of that value like so:
plan1.discountMonths[2] = 10;
That's it, that would change the third item in the array (index value 2), to 10 (November). This works also:
plan1.name = "More than Basic";
That would change the value of the name property from "Basic" to "More than Basic." To create an object without any properties, you can do this:
var plan2 = {};
You can insert an property into the object and give it a value of undefined, like so:
plan2.price = undefined;
Undefined should not be in quotation marks, because it is not a string. If you want to delete a property of an object, you can do so like this:
delete plan2.price;
Note that that would delete the actual property (price), along with any attached value, such as undefined in the case above. You can check if a property exists in an object, like so:
propertyExists = "space" in plan2;
JavaScript will return a value of true or false for the code above.
When a variable is attached to to an object, the variable becomes the object's property. When a function is attached to an object, the function becomes that object's method. To do so, you would do the code like this:
var plan1 = {
name: "Basic",
price: 3.99,
space: 100,
transfer: 1000,
pages: 10,
calcSomething: function() {
}
};
The function code with any variables necessary, would be located within the function. It's basically the same as creating a function, except for the property name and colon preceding it the function instead of setting up the function like:
function calcSomething() {
}
This is how you'd call the method above:
plan2.calcSomething();
You could assign it to a variable, like so:
var priceOfSubscription: plan2.calcSomething();
You could put an amount in the parens if you happened to set up the function to use the value within the parens as an input within the function. When you make a function within an object (a method), you can refer to a desired value like so within the function:
plan2.name
For example. But the author suggests that, within the function, we use this.name, and that JavaScript will know we are referring to the object within which the function is being inserted/defined.
Next I went over constructor functions, which prevent us from having to do this over and over:
var plan1 = {
name: "Basic",
price: 3.99,
space: 100,
transfer: 1000,
pages: 10,
calcSomething: function() {
}
};
Instead, a constructor function allows us to create dozens of these objects with much more ease, like so:
function Plan(name, price, space, transfer, pages) {
this.name = name,
this.price = price,
this.space = space,
this.transfer = transfer,
this.pages = pages;
};
Note that plan is capitalized, if the name of the function is capitalized, that lets other devs know that the function is a constructor function. Also, note the semicolon after the last value name. What this code is doing is it's creating a function, and in the future, all you do is input the values for the properties in the function, and it creates objects with that information. This is how you would create objects with the function above:
var plan1 = new Plan("Basic", 3.99, 100, 1000, 10);
So first you make the constructor function, then you call it with the code above to create new objects...now I'm wondering how you can insert a function as a property via the use of a constructor function, that is to say, a method...
So, once you've set up your constructor function, you could do this:
var plan1 = new Plan("Basic", 3.99, 100, 1000, 10);
var plan2 = new Plan("Cool", 3.99, 100, 1000, 10);
var plan3 = new Plan("Amazing", 3.99, 100, 1000, 10);
That constructor function would save a lot of work, as opposed to setting up the object and variables by hand each and every time. "new" is the keyword that tells JavaScript to create a new object.
It's common to use the same name for parameters and properties, like in the code above, but you don't have to, you could do this:
function Plan(name, price, space, transfer, pages) {
this.why = name,
this.what = price,
this.how = space,
this.when = transfer,
this.noidea = pages;
}
That would be confusing, though, so we should not do that.
Oh, cool, the next chapter, chapter 73, covers inserting methods into constructor functions! Here's how:
function Plan(name, price, space, transfer, pages) {
this.why = name,
this.what = price,
this.how = space,
this.when = transfer,
this.noidea = pages;
this.calcSomething = function() {
};
}
So there's no extra input in the parameters to deal with the method, the method is simply inserted at the end of the list of properties.
Once the objects are created, again, like so:
var plan1 = new Plan("Basic", 3.99, 100, 1000, 10);
var plan2 = new Plan("Cool", 3.99, 100, 1000, 10);
var plan3 = new Plan("Amazing", 3.99, 100, 1000, 10);
There is no input within the constructor function in regards to the method within. We would find out the annual price (as in, we would input a value into the method) like this:
var annualPrice = plan3.calcSomething();
If the function incorporated a variable in the parameter, then we would enter the value for that variable when calling the function above. So it's the object, followed by a period, then the function name, with the parameter within, if appropriate.
So, if we're creating a method within an object definition, it goes like so:
calcSomething: function() {
On the other hand, when we create a method within a constructor function, the format is like so:
this.calcSomething = function() {
This is really neat! Also, remember that if a function is being defined in a standalone fashion, there is no semicolon after the curly braces, but if a function(method) is being defined within a constructor function, because of the equal sign, there needs to be a semicolon after the closing curly brace for the method (but not for the constructor function). Also, even if another property or method definition follows the method in the constructor function (that is, even if it's not last), no comma is needed (the semicolon is there anyways). So, to be clear, no comma is needed after a method within a constructor function, regardless of it's order within the properties of the object being created. Also, note that a method inside of a constructor function is not considered a property of that function, it is a method of that constructor function.
I finished chapter 73, and now I'm on Chapter 74 Objects: Prototypes. A prototype statement is a way to avoid having the code for a method be included over and over in every object (remember the DRY principle, Don't Repeat Yourself).
First, as you can imagine, we don't include the method in the constructor function (otherwise it gets repeated every time). This is how:
Plan.prototype.calcSomething = function(someVariableIfDesiredOrEmptyIfNot) {
};
So, what that does is it includes the calcSomething function inside of every Plan object. Everything except for the first line is the same as when the function is included as a method of the constructor function.
You can also create a prototype property instead of a prototype method. You would do that like this:
Plan.prototype.price = 5.00;
So then every object created with the constructor function Plan would have the property, price, with a value of 5.00. That would create a lot less code, and is useful when the objects created have a property (or method) which is the same for every object.
You can also override a prototype for one particular object, like so:
plan3.price = 8.00;
That's the object name, followed by the property and then a value. So then every object created with the Plan constructor function would have a price of 5.00, except for the object plan3, which would have a price of 8.00.
In the same way, you can override a shared prototype method for individual objects.
A regular function does not have a semicolon after the curly braces.
A constructor function also does not have a semicolon after the closing curly brace.
A prototype with a method does have a semicolon after the curly brace.
Okay, I finished that chapter and now I'm working on Chapter 75 Objects: Checking for Properties and Methods.
We can check to see if an object has a property within it like so:
var gotTheProp = "storage" in plan3;
The result will be a boolean, true or false.
Oh, and it turns out a method is a type of property of an object after all...
We could look for a method in the object Plan3 like so:
var gotTheProp = "calcSomething" in Plan3;
Just make sure to use the quotation marks, even though calcSomething is not a string. Here's how to get a list of properties in an object:
var listOfProps = [];
We just made an empty array. Then:
for (var prop in Plan3) {
listOfProps.push(prop);
}
This is really odd code, because the loop stops by itself when it's out of variables. The result would be an array with a bunch of property names inside (but not the property values). Also, you can use any variable name instead of prop (in the first parens). This array will include properties that were inherited via a prototype. If you want to exclude those properties, use this code:
var listOfProps = [];
for (var prop in Plan3) {
if (plan3.hasOwnProperty(prop)) {
listOfProps.push(prop);
}
}
Okay, I finished chapter 75, so now I'm starting with Chapter 76 Browser Control: Getting and Setting the URL.
window.location.href will return the complete web address you are currently on, in a string. This is also known as the URL.
window.location.hostname will return just the domain name in a string, for example, "www.samplesite.com."
window.location.pathname will return the path, which is the text after the .com (as an example). If the browser is on the site's home page, this will return "/". It won't return the anchor though. The anchor is the # followed by text at the end of some paths.
window.location.hash will return the anchor, for example, #randomanchor at the end of the path. If there is no anchor, an empty string will be returned, "".
Okay, I'm calling it a day, I'll continue with chapter 76 tomorrow!
SUMMARY OF CODING SKILLS
Total Treehouse Points: 5,385
Treehouse Points by Subject Matter (Miscellaneous not included):
HTML: 663
CSS: 1,599
Design: 1,193
Development Tools: 747
Javascript: 1,120
Treehouse Ranking (%): "You have more total points than 94% of all students."
Treehouse Badge(s) Earned Today:
Treehouse Courses Completed:
How to Make a Website
HTML
CSS Foundations
CSS Layout Techniques
Aesthetic Foundations
Design Foundations
Adobe Photoshop Foundations
Adobe Illustrator Foundations (66% complete, switched focus from web design to web dev)
Git Basics
Introduction to Programming
Javascript Basics
Codecademy (& other) Courses Completed:
HTML and CSS (Codecademy)
Design Foundations
Adobe Photoshop Foundations
Adobe Illustrator Foundations (66% complete, switched focus from web design to web dev)
Git Basics
Introduction to Programming
Javascript Basics
Codecademy (& other) Courses Completed:
HTML and CSS (Codecademy)
Books Read or in Progress:
Completed: "Head First HTML and CSS," by E. Robson & E. Freeman
In Progress: "Eloquent JavaScript," by Marijn Haverbeke (On pg 27)
In Progress: "Head First Javascript," by Eric Freeman and Elisabeth Robson (On pg 56)
In Progress: "A Smarter Way to Learn Javascript," by Mark Myers (on pg 255)
My Progress on The Odin Project:
1. Introduction to Web Development 100% Complete
2. Web Development 101 29% Complete
3. Ruby Programming 0% Complete
4. Ruby on Rails 0% Complete
5. HTML5 and CSS3 0% Complete
6. Javascript and JQuery 0% Complete
7. Getting Hired as a Web Developer 0% Complete
Hours Spent Coding Today: 4
Total Hours Coding: 606