blogatom.

  • Archives
  • RSS

JS Inheritance in 15mins or Less

beardedocto:

Inheritance and encapsulation is an essential tennant of OOP. JS does things a little bit differently than most languages, we’ll go over those differences in this post. It’s assumed you already have a basic familiarty w/ OO JS, if not head over my OO JS in 15mins or Less tutorial to cover the basics.

Classes

JavaScript technically doesn’t have classes but it does have constructors. Constructors act as containers for class variables and methods just like the class statement in other languages. Let’s start by creating a constructor function.


function Person() {};

Looks good, to bad it’s empty. Let’s tack on an instance variable and a method that can access that instance variable w/ prototype (Need a crash course on prototype in JS? See the posts by Yehuda Katz or Angus Croll).


Person.prototype.type = "Human";
Person.prototype.sayType = function() {
    console.log("Type: " this.type);
}
var me = new Person();
me.sayType(); // "Type: Human"

Read More

  • il y a 1 an > beardedocto
  • 7
  • Permalien
Share

Adresse courte

TwitterFacebookPinterestGoogle+

7 Notes/ Hide

  1. russmatney aime ce billet
  2. gregbabula aime ce billet
  3. blogatom a reblogué ce billet depuis beardedocto
  4. blogatom aime ce billet
  5. grardb aime ce billet
  6. stevekinney aime ce billet
  7. techniqual a reblogué ce billet depuis beardedocto
  8. beardedocto a publié ce billet
← Précédent • Suivant →

blogatom.

À propos

space dimension music percussion life illusion future dimension dragons invasion computer explosion vacuum exploration spanish inquisition tubular incantations

Me, Elsewhere

  • @tomconte on Twitter
  • Facebook Profile
  • tomconte on Youtube
  • tomconte on Flickr
  • tomconte on Soundcloud
  • tomconte on Foursquare
  • My Skype Info
  • Linkedin Profile
  • tomconte on github

Twitter

Instagram

loading tweets…

loading photos…

  • RSS
  • Au hasard
  • Archives
  • Mobile
Effector Theme by Pixel Union