When you come to Javascript from the Ruby world, things can be pretty confusing and strange. What are all these curly braces and semi-colons for? Why are there some functions with no names? These are a few of the questions that went through my mind when I first encountered Javascript. Although Javascript is insanely powerful with things like jQuery and AngularJS, it’s a pretty tough language to get used to compared to Ruby.

Calling functions

To call a function or method in Ruby, you can use parentheses for calling a method, or leave them out:

```ruby Methods

def hello puts “Hello.” end

hello() # prints out “Hello.”

hello # also prints out “Hello.”


In Javascript, you must use parentheses to invoke a method. This is because in Javascript, functions can be passed as data to other functions and wait to be called later, hence their name: callback function. 

```javascript Invoking functions

var hello = function() {
  console.log("Hello")
}

hello() // prints out "Hello"

// Here, a callback is passed in as an argument, but not invoked until it is used in the function
var anotherHello = function(callback) {
  callback()
}

anotherHello(hello) // also prints out "Hello"

If you tried to do the same thing in Ruby, the method would get invoked in the argument list, and the argument would end up being the value that the function returns, rather than the function itself. It is possible to use functions as data in Ruby, though, with a lambda:

```ruby Lambda

-> is the lambda operator

a_lambda = -> { puts “hello” }

You can now pass the lambda around as a variable and call it when you need it with the #call method.

def call_a_lambda(a_lambda) a_lambda.call end


This is how ActiveRecord callbacks work in Rails.

### Obect Orientation ###

If you're working with Rails, chances are you won't have to deal with much object-oriented Javascript. But, if you want to create a game, or use Javascript for your full stack, you will probably encounter the need to use OOP. The way Javascript deals with objects is a lot different than Ruby. Rather than the <code>class</code> acting as a blueprint for objects like in Ruby, functions and data are encapsulated within a dictionary, which is similar to the concept of a hash in Ruby.  

The simplest way to define an object in Javascript is with curly braces:

```javascript Simple object

var object = {}

You can then add behavior and data to that object by setting property names:

```javascript Person

var person = {} person.name = “Garrett” person.say = function() { alert(“Hello, my name is, “ + person.name ) }


This is fine, but it is an object modeled after a person, not a <code>Person</code> object. In Ruby, this would be akin to setting a variable to be a new <code>Object</code> and setting singleton methods on that single instance. If you want to create a blueprint for your objects, like a class, you can use two different constructs: a closure or a prototype.

A closure is simply an all encompassing function that defines variables and methods in the same context:

```javascript Closure

var Person = function(name) {
  this.name = name
  this.say = function() {
    alert("Hello, my name is" + this.name)
  }
}

You can then create a new instance of this Person object by using the new object constructor:

```javascript New Person

var person = new Person(“Garrett”)

person.say() // Hello, my name is Garrett



This may seem more straightforward, but it comes at a price; using closures to deal with objects uses more memory, since every property and function of the object is re-created for every instance of the class that you create, instead of referring to a separate class definition.

This is where the prototype object comes in. Here's our <code>Person</code> class again, using the prototype object:

```javascript Person using prototype

var Person = function(name) {
  // This is just the constructor. We just want to initialize a
  // Person object with a name.
  this.name = name
}


Person.prototype.say = function() {
  alert("Hello, my name is" + this.name)
}

Using an object’s prototype is similar to using a Ruby class; the prototype acts as a blueprint for instances of the class.

Inheritance

Inheritance is also weird in Javascript:

```javascript Inheritance

// The class we want to inherit from function Parent() {} Parent.prototype.name = “Parent”

function Child() { Parent.call(this) // You have to “call” the Parent object within the Child object. }

// Set the Child’s prototype object to a new Parent object. Child.prototype = new Parent()

// Set the Child’s constructor to Child, because it was overridden when you copied the Parent’s prototype. Child.prototype.constructor = Child


Now, you can create a new <code>Child</code> object that inherits behavior from the <code>Parent</code> prototype:

```javascript New Child

var child = new Child
child.name // Still "Parent", but you can override it now.

child.name = "Child"

There are a ton a things that are weird about Javascript, but these were some things that gave me some real trouble understanding.