Skip to content

Instantly share code, notes, and snippets.

@awkale
Last active December 14, 2015 15:14
Show Gist options
  • Select an option

  • Save awkale/3da2c88bf83b0c3e0773 to your computer and use it in GitHub Desktop.

Select an option

Save awkale/3da2c88bf83b0c3e0773 to your computer and use it in GitHub Desktop.
reusable object in javascript; uses a function
(function() { // protect namespace, Immediately-Invoked Function Expression (IIFE)
function Person( firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
Person.prototype.greet = function() { // prototypes allows object to inherit function properties
return "hello my name is " + this.firstName;
}
Person.prototype.farm = function() {
console.log('look ma! I\'m farming!');
}
var alex = new Person( 'Alex', 'Kale' );
console.log( alex, alex.greet() );
var GrumpyCat = new Person( 'Grumpy', 'Cat' );
console.log( GrumpyCat );
// inheritance
function Dog() {
this.numLegs = 4;
this.fur = true;
}
Dog.prototpye.barks = 'loudly';
function Pug() {
}
Pug.prototype = Dog.prototype;
Pug.prototype.sleep = function() {
console.log('2 hours');
}
var jack = new Pug();
console.log(jack);
Dog.prototype.foo = 1;
console.log( jack );
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment