Advertisement
imehesz

Simple Inheritance with JavaScript/CoffeeScript

Sep 16th, 2014
448
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Simple Inheritance
  2. # based on http://blogs.msdn.com/b/eternalcoding/archive/2014/08/20/simple-inheritance-with-javascript.aspx?utm_source=javascriptweekly&utm_medium=email
  3.  
  4. inheritesFrom = (child,parent) ->
  5.   child.prototype = Object.create parent.prototype
  6.  
  7. ClassA = ->
  8.   @.name = "class A"
  9.  
  10. ClassA.prototype.print = ->
  11.   console.log @.name
  12. a = new ClassA()
  13.  
  14. ClassB = ->
  15.   @.name = "class B"
  16.   @.surname = "I am the child"
  17.  
  18. inheritesFrom(ClassB, ClassA)
  19.  
  20. b = new ClassB()
  21.  
  22. ClassB.prototype.print = ->
  23.   ClassA.prototype.print.call(@)
  24.   console.log @.surname
  25.  
  26. b.print()
  27.  
  28. ClassC = ->
  29.   @.name = "Class C"
  30.   @.surname = "I am the grand-child"
  31. inheritesFrom(ClassC, ClassB)
  32.  
  33. ClassC.prototype.print = ->
  34.   ClassB.prototype.print.call(@)
  35.   console.log "everything is awesome ..."
  36.  
  37. c = new ClassC()
  38. c.print()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement