Last active
February 21, 2017 16:07
-
-
Save panw/b7663f05d830880dd3168eef88d298aa to your computer and use it in GitHub Desktop.
Ruby to JavaScript Overview
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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