Advertisement
Guest User

Untitled

a guest
Aug 3rd, 2015
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.54 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>"this" context explained - Object Constructors</title>
  5. </head>
  6. <body>
  7.  
  8. <script type="text/javascript">
  9. /*
  10. * * "this" context and object constructor functions -
  11. * * like classes but they ain't!
  12. */
  13.  
  14. //Create an object constructor
  15. function User ( fname, lname, eml ) {
  16. this.firstname = fname;
  17. this.surname = lname;
  18. this.email = eml || "";
  19. }
  20.  
  21. /*
  22. * * Adding methods to the prototype object allows methods be shared [inherited]
  23. * * across all User object instances.
  24. */
  25. User.prototype = {
  26. "getMyName": function () {
  27. return this.firstname + "_" + this.surname;
  28. },
  29. "getEmailAddress": function () {
  30. return "User constructor email is " + this.email;
  31. }
  32. };
  33. /*
  34. * * Object constructors are functions but when call with the "new" keyword,
  35. * * "this" points to the object instantiating it, which in this example,
  36. * * is tilly and brad.
  37. */
  38. var tilly = new User( "Tilly", "Lee", "tlee@gmail.com" ),
  39. brad = new User( "Brad", "Jenkins", "bradjenkins@hotmail.com" );
  40.  
  41. /*
  42. * * The "new" also returns an object instance of the User object
  43. * * to the object instance, making all properties and methods
  44. * * accessible
  45. */
  46. console.log( tilly.firstname );//returns "Tilly"
  47. console.log( brad.surname );//returns "Jenkins"
  48. console.log( brad.getMyName() );//returns "Brad_Jenkins"
  49.  
  50. /*
  51. * * [Extension]
  52. * * You can add new properties and methods to the User object which
  53. * * are automatically inherited by object instances
  54. */
  55. User.prototype.getCompanyName = function () {
  56. return "Google Inc";
  57. };
  58. console.log( tilly.getCompanyName() );//returns "Google Inc"
  59. console.log( brad.getCompanyName() );//returns "Google Inc"
  60.  
  61. /*
  62. * * And you can add properties and methods exclusively to
  63. * * object instances as they are added to the object instance
  64. * * itself and NOT the object prototype of its parent
  65. */
  66. tilly.starsign = "Leo";
  67. tilly.getStarsign = function () {
  68. return this.starsign;
  69. };
  70. console.log( tilly.getStarsign() );//returns "Leo"
  71. //console.log(brad.getStarsign());//returns "Uncaught TypeError: brad.getStarsign is not a function"
  72.  
  73. /*
  74. * * [Polymorphism]
  75. * * You can also change the implementation of a method
  76. * * exclusively on any object instance without affecting
  77. * * the parent and other instances of that parent.
  78. * *
  79. * * If you know that other object instances with require
  80. * * the same altered method, create another constructor
  81. * * object derived from its parent and implement the
  82. * * alternate method there to avoid duplicate code
  83. */
  84. brad.getCompanyName = function () {
  85. return "Facebook Inc";
  86. };
  87. console.log( tilly.getCompanyName() );//returns "Google Inc"
  88. console.log( brad.getCompanyName() );//returns "Facebook Inc"
  89.  
  90.  
  91. /*
  92. * * {Pro}: "this" context is bound to the object instance;
  93. * * {Pro}: Good for apps which require multiple objects to work with;
  94. * * {Pro}: Methods are easily shared as each instance inherits it's
  95. * * own copy of methods as listed in the object.prototype object;
  96. * * {Pro}: Each object instance can change the implementation of its
  97. * * own method without affecting other object instances
  98. * * {Pro}: If a method or property is added, removed or altered at
  99. * * the parent object, all instance objects are automatically updated
  100. * * with the new method;
  101. * * {Con}: All properties and methods are accessible which means
  102. * * that properties and methods can easily be overridden
  103. * * {Con}: Unable to share private variables [unless you do prepend an
  104. * * underscore to the property name -> _this.propName]
  105. */
  106.  
  107. /*
  108. * * What happens when the "new" keyword is omitted when instantiating User?
  109. */
  110. var kim = User( "Kim", "Tran", "kimtran@yahoo.com" );//Uh Oh no "new" keyword
  111. console.log( kim );
  112. /*
  113. * * Result, executes alright but, returns undefined. kim did however
  114. * * create firstname, surname, and email variables but they've been
  115. * * added to the global window.
  116. * * Why is this bad? If global functions refer to such variables and
  117. * * fail to use the "var" keyword, the result of these functions may
  118. * * not be the outcome you expect and will cause confusion
  119. */
  120. console.log( firstname );//returns "Kim"
  121. console.log( surname );//returns "Tran".
  122.  
  123.  
  124. </script>
  125.  
  126. </body>
  127. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement