#Objectified: a very brief overview of Objects in Javascript
written for myself, by myself.
much love,
Nick
var exampleObject = {
property : "value",
method : function(argument) {
console.log("Whoah, objects can have functions *inside* them!");
}
}; // exampleObject
Objects can contain any number of things (variables, arrays, functions).
note, a scond way to create exampleObject is through "dot notation" :
var exampleObject;
exampleObject.property = "value";
exampleObject.method = function(argument) {
console.log("Whoah, objects can have functions *inside* them!");
};
here's an example object that we will be using through this doc:
var jack = {name: "jack", age: 40};
we can access it through a number of methods:
console.log(jack["name"]);
or
console.log(jack.name); // prefered (because it is shorter)
Now, onto a more efficient way to make objects!
Often -- nay extremely often -- you will want to recreate objects with the same type of properties, values, or functions. One could do it like this:
var jill = {
name: "jack",
age: 32
}
var jill = {
name: "Jill",
age: 40
}
// etc...
but that would be pretty tedious, woudldn't it?
Instead we can creat a function to do the legwork for us. This is called a constructor
function Person(name, age) {
this.name = name; // the "this" refers to whatever object is being created and assigns it's
this.age = age;
this.sayName = function() {
console.log("Hi, my name is " + this.name);
};
}
var jack = new Person("jack", 32); // creates a new object "jack" with the properties of our Class
jack.sayName(); // will call Sayname method
remember the typeOf function? Well, we can apply it to the properties of objects as well, using bracket notation. Yay for old friends.
// (remember that jack's name is "jack" and his age is "32")
if ( typeof jack["age"] === "number") {
console.log("it's a number!");
} else if (typeof jack["age"] === "string") {
console.log("It's a string baby!");
}
Consider the following: function Person(name, age) { this.name = name; // the "this" refers to whatever object is being created and assigns it's this.age = age; this.sayName = function() { console.log("Hi, my name is " + this.name); }; }
var jack = new Person("jack", 32); // creates a new object "jack" with the properties of our Class
var jill = new Person("jill", 40);
var zeqad = new Person("zeqad", 700);
Suppose we wanted to print off all the ages of our people, but not have to write console.log(person.age) for all of them? You're probably thinking that we will employ some sort of loop, like we to defeat those nefarious arrays. You are right.
for (var x in jack) { // for as many properties as there are in the object jack console.log(jack[x]); // print out the property value }
Obviously, this itself can be turned into a function:
function propertyScanner(object) {
for(var x in object) {
console.log(object[x]);
}
}
propertyScanner(jack);
in our previous example, we created a Person constructor, and used it to create 3 objects: Jack, Jill, and Zeqad.
Those 3 person objects all belong to the "Person" class
Just as our 3 friends belong to the "Person" class, "Person" belongs to the Object Prototype (which javascript uses to measure all objects)
- object prototype
- class (person)
- object (jack)
- class (person)
- person
- programer
- jill
- stylist
- jack
- xylophonist
- Zeqad
- programer
###$testing for properties
// what is this "Object.prototype" anyway...?
var prototypeType = typeof Object.prototype;
console.log(prototypeType);
// now let's examine it!
var hasOwn = Object.prototype.hasOwnProperty("hasOwnProperty");
console.log(hasOwn);