Diving into JavaScript

Post-Ruby!

May 24, 2015


Initially, my adjustment from Java / JavaScript to Ruby was a little rocky. With JavaScript, you have to be careful about semicolons, curly braces, and placement of commas in respect to object properties. But Ruby is far more forgiving.After a while, I didn't miss having to use semicolons or curly braces. I'm really loving Ruby. It's beautiful, clean, and easy to read.

I will quickly go over one of the differences between Ruby and JavaScript in terms of: classes! (we can do syntax later)

In Ruby, the syntax and format for creating a class is really straightforward. Here's a sample class:


          
            class Employee
              attr_accessor :firstName, :lastName, :title

              def initialize(firstName, lastName, title)
                 @firstName = firstName
                 @lastName = lastName
                 @title = title
              end

              def display
                 puts "Employee is " + firstName + " " + lastName
              end
            end

            christine = Employee.new("Christine","Schatz",
            "software engineer")
            christine.display # Employee is Christine Schatz
          
        

In JavaScript, classes are trickier. You will never see the word "class." Rather, you'll see what look like functions, the first here of which is called a "constructor.":


          
            function Employee(firstName, lastName, title) {
              this.firstName = firstName;
              this.lastName = lastName;
              this.title = title;
            }

            Employee.prototype.fullName = function() {
              return this.firstName + " " + this.lastName;
            }

            var christine = new Employee("Christine", "Schatz",
            "software engineer");
            christine.fullName(); // "Christine Schatz"
           
         

That's all for now! Thanks for reading :-)