Guest User

Untitled

a guest
Feb 18th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. $(document).ready(function () {
  2. let example;
  3.  
  4. $.getJSON( "setting.json", function(data) {
  5.  
  6. // create object based on settings
  7. example = new ExampleObject(data);
  8.  
  9. }).fail(function(e) {
  10. console.warn(e);
  11. }).always(function() {
  12. console.log("ExampleObject after json call: ");
  13. console.log(example);
  14.  
  15. window.exampleObject = example; // is still undefined in other js file cause that runs before this finishes.
  16. });
  17.  
  18. window.exampleObject = example; // is undefined cause json fetch finishes after setting this.
  19. });
  20.  
  21. class ExampleObject{
  22. constructor(settings){
  23. this.width = settings.width;
  24. this.height = settings.height;
  25. }
  26. setWidth(width){
  27. this.width = width;
  28. }
  29. setHeight(height){
  30. this.height = height;
  31. }
  32. }
  33.  
  34. {
  35. "width": 3,
  36. "height": 4
  37. }
  38.  
  39. $(document).ready(function () {
  40.  
  41. // get object from global object
  42. console.log(window.exampleObject);
  43.  
  44. // cant run this line since window.exampleObject is undefined.
  45. window.exampleObject.setWidth(6);
  46. console.log(window.exampleObject);
  47. });
Add Comment
Please, Sign In to add comment