Js Objects Intro


  • Explain what an object is in JavaScript
  • Explain the parts of a JS object
  • Create an object in JS using literal syntax

Ruby Versus JavaScript

You've already worked with objects in JavaScript, but so far we've only treated them like hashes. Now it's time to change frame of mind and start to view them as objects with properties and values, just like the objects we make in Ruby using classes.

This frame of reference from Ruby is going to make dealing with objects in JavaScript that much easier. You already know what an object is, so now it's time to learn how JavaScript handles them.

Let's create a user class and object in Ruby. The user will have a name and email attribute:

class User
  attr_accessor :name, :email

  def initialize(name, email)
    @name = name
    @email = email

kevin = User.new("kevin", "kevin@aol.com")

Now let's recreate the same thing in JS:

var kevin = {
  name: "kevin",
  email: "kevin@aol.com"

Properties and Data

How come when we create our kevin JavaScript object, it looks just like a hash but in Ruby our kevin object looks more like what we typically think of as an object?

Objects in Javascript are a lot more versatile than they are in Ruby. Essentially hashes in Javascript are objects, but they can also have functions attached to them (methods). In Ruby we think of Objects and Hashes as fundamentally different ideas (technically Hashes in Ruby are objects). However, in Javascript, we can use objects like Hashes, just as a set of key value pairs, or we can attach functions to them and use them more like we traditionally think of using objects in Ruby. In Ruby objects contain both data and behavior and we'll soon learn to use our Javascript objects in the same manner.

View Object Oriented Javascript on Learn.co and start learning to code for free.

View Object Oriented Javascript on Learn.co and start learning to code for free.

Unlock your future in tech
Learn to code.

Learn about Flatiron School's Mission

With a new take on education that falls somewhere between self-taught prodigy and four-year computer science degree, the Flatiron School promises to turn students with little programming experience into developers.

In the six months since the Manhattan coding school was acquired by WeWork, it has spawned locations in Washington, D.C., Brooklyn, and London. Now, WeWork is opening a fourth Flatiron School location, this time in Houston.

Adam Enbar, Flatiron School's cofounder, believes now is the time to grow. "How the world is changing has impacted working and learning in very similar ways. We think education fundamentally is about one thing: enabling people to pursue a better life."

Learn. Love. Code.
Students come to Flatiron School to change their lives. Join our driven community of career-changers and master the skills you need to become a software engineer or a data scientist.
Find Us