Created
July 7, 2015 13:15
-
-
Save johnantoni/da6764f16298431bf0c8 to your computer and use it in GitHub Desktop.
javascript functions
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
// ------ functions with closure | |
var y = 12345; | |
var z = 12345; | |
(function (y) { | |
var x = "Hello!!"; | |
console.log(y) | |
})(); | |
// ------ functions with prototypes | |
var root = { | |
speak: function(y) { | |
console.log(this.name + ' ' + y); | |
} | |
}; | |
var newRoot = Object.create(root); | |
root.name = 'sally'; | |
root.speak('peter'); | |
// ------ functions with constructors | |
function Rabbit(type) { | |
this.type = type; | |
this.speak = function(line) { | |
console.log(line); | |
}; | |
} | |
var newRabbit = new Rabbit('rabbit'); | |
console.log(newRabbit.type); | |
Rabbit.prototype.speak = function(line) { | |
console.log(line + 'proto'); | |
}; | |
newRabbit.speak('arg'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment