Skip to content

Instantly share code, notes, and snippets.

@NickTomlin
Created April 23, 2012 18:53
Show Gist options
  • Save NickTomlin/2473045 to your computer and use it in GitHub Desktop.
Save NickTomlin/2473045 to your computer and use it in GitHub Desktop.
#Objectified: a very brief overview of Objects in Javascript

#Objectified: a very brief overview of Objects in Javascript written for myself, by myself.
much love,
Nick


An introduction to objects

So, here's an object:

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!"); 
	};

Accessing the properties of an object

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:

Bracket notation
console.log(jack["name"]);

or

Dot notation
console.log(jack.name); // prefered (because it is shorter)

Now, onto a more efficient way to make objects!

Object constructors

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

A basic 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

A brief detour into typeof...

remember the typeOf function? Well, we can apply it to the properties of objects as well, using bracket notation. Yay for old friends.

Let's use typeof to detect if jack's age is a string or not:
// (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!");
}

Looping in to objects

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.

introducing the for/in loop

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);

Objects in object oriented programming OOP

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

prototypes

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)

an example of the oop model:

  • person
    • programer
      • jill
    • stylist
      • jack
    • xylophonist
      • Zeqad

errata

###$testing for properties

The object.prototype

testing to see if the object prototype has the hasOwnProperty property! Whoah.

// 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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment