Skip to content

Instantly share code, notes, and snippets.

@johnantoni
Created July 7, 2015 13:15
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 johnantoni/da6764f16298431bf0c8 to your computer and use it in GitHub Desktop.
Save johnantoni/da6764f16298431bf0c8 to your computer and use it in GitHub Desktop.
javascript functions
// ------ 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