Skip to content

Instantly share code, notes, and snippets.

@ashleygwilliams
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ashleygwilliams/9318125 to your computer and use it in GitHub Desktop.
Save ashleygwilliams/9318125 to your computer and use it in GitHub Desktop.
//object literal notion
var my_obj = {
attr1: 'hello',
attr2: 'what'
};
//data is accessible by using keyword via dot notation...
my_obj.attr1; // => 'hello'
//...or brackets
my_obj['attr1'];
//all objects have a __proto__ property
my_obj.__proto__; // => {}
//we can add properties to __proto__
my_obj.__proto__.attr0 = 'wow';
my_obj.__proto__; // => { attr0: 'wow' }
// ...and my_obj will inherit them
my_obj.attr0; // => 'wow'
//prototypes have prototypes
//if no prototype is specifcied,
my_obj.__proto__.__proto__; // =>null
# ruby hashes look nearly identical to js object literals
my_hash = {
attr1: 'hello',
attr2: 'what'
}
#can access values by using brackets
my_hash[:attr1] # => 'hello'Object.prototype is used
#inherits properties from a class
my_hash.class # => Hash
#To make an object in Ruby we need a class
class MyObject
attr_accessor :attr1, :attr2
def initialize(x, y)
@attr1 = "x"
@attr2 = "y"
end
end
#Ruby objects are made using the new keyword
my_obj = MyObject.new("hello", "what")
class MyObject
def quack
puts "quack!"
end
end
my_obj.quack # "quack!"
#Objects inherit from each other
my_obj.class #=> MyObject
My_Object.superclass #=> Object
Object.superclass #=> BasicObject
BasicObject.superclass #=> nil
function myClass() {
this.array = [];
}
function myObject() {
this.attr1 = "hello";
}
myObject.prototype = new myClass();
myObject.prototype.constructor = myObject;
var obj1 = new myObject, obj2 = new myObject;
obj1 // =>{ attr1: 'hello' }
obj1.array // =>[]
obj1.array.push(1);
obj2.array //=> [ 1 ]
#To make an object in Ruby we need a class
class MyObject
attr_accessor :attr1, :attr2
def initialize(x, y)
@attr1 = "x"
@attr2 = "y"
end
end
#Ruby objects are made using the new keyword
my_obj = MyObject.new("hello", "what")
class MyObject
def quack
puts "quack!"
end
end
my_obj.quack # "quack!"
#Objects inherit from each other
my_obj.class #=> MyObject
My_Object.superclass #=> Object
Object.superclass #=> BasicObject
BasicObject.superclass #=> nil
#To make an object in Ruby we need a class
class MyObject
def initialize(x, y)
@attr1 = "x"
@attr2 = "y"
end
end
#Classes are also objects and have their own inheritance tree
My_Object.class #=> Class
Class.superclass #=> Module
Module.superclass #=> Object
Object.superclass #=>BasicObject
BasicObject.superclass #=> nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment