Last active
December 14, 2015 15:14
-
-
Save awkale/3da2c88bf83b0c3e0773 to your computer and use it in GitHub Desktop.
reusable object in javascript; uses a function
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
| (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