Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2014
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var Human = function( name )
  2. {
  3.     var parent = this;
  4.     this.init = function( name )
  5.     {
  6.         parent.name = name;
  7.     }( name );
  8. }
  9.  
  10. var ardi = new Human( "Ardi" );
  11.  
  12. ardi.name (Is inaccessible)
  13.  
  14. Because it retains "bases"' "this" value, eg:
  15. ardi.built or ardi.getEnemies() (Will have a value)
  16.  
  17. ^ This is not how JavaScript works.
  18.  
  19. <Only proper workaround>
  20.  
  21. var Human = function( name )
  22. {
  23.     var fakeThis = {};
  24.    
  25.     fakeThis.init = function( name )
  26.     {
  27.         fakeThis.name = name;
  28.     }( name );
  29.    
  30.     return fakeThis;
  31. }
  32.  
  33. ^ This is also unreliable hack since some of the things get fucked
  34.  
  35.  
  36. ---WHAT WE SHOULD HAVE---
  37. Is template similar to this:
  38.  
  39. var Human = function()
  40. {
  41.     var parent = this;
  42.     this.init = function( name )
  43.     {
  44.         parent.name = this;
  45.     }( name );
  46. }
  47.  
  48. this.chooseAction = function()
  49. {
  50.     /* Choose action logic here, that gets called by your game engine */
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement