Skip to content

Instantly share code, notes, and snippets.

@panw
Last active February 21, 2017 16:07
Show Gist options
  • Save panw/b7663f05d830880dd3168eef88d298aa to your computer and use it in GitHub Desktop.
Save panw/b7663f05d830880dd3168eef88d298aa to your computer and use it in GitHub Desktop.
Ruby to JavaScript Overview
var numbers = [1, 2, 3];
numbers.forEach(function(num, index, array) {
console.log('I am ' + num)
})
var doubledNumbers = numbers.map(function(num) {
return num * 2
});
var total = numbers.reduce(function(sum, num) { return sum + num }, 0)
var keyValuePairs = {
k1: 'v1',
k2: 'v2'
}
for(key in keyValuePairs) {
console.log(keyValuePairs[key])
}
function Fish(name, species) {
this.name = name;
this.species = species;
this.milesTravelled = 0;
}
Fish.prototype.swim = function() {
this.milesTravelled++;
}
var nemo = new Fish('Nemo', 'clown fish')
// AnglerFish's Prototype Chain
// null -> object -> Fish -> AnglerFish
function AnglerFish(name, species, gender) {
Fish.call(this);
this.name = name;
this.species = species;
this.gender = gender;
this.mates = [];
}
AnglerFish.prototype.consume = function(maleFish) {
if(this.gender === 'female') {
this.mates.push(maleFish);
}
}
var dory = new AnglerFish('Dory', 'Angler', 'female')
var nemo = new AnglerFish('Nemo', 'Angler', 'male')
dory.swim()
dory.consume(nemo)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment