Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $(document).ready(function () {
- let example;
- $.getJSON( "setting.json", function(data) {
- // create object based on settings
- example = new ExampleObject(data);
- }).fail(function(e) {
- console.warn(e);
- }).always(function() {
- console.log("ExampleObject after json call: ");
- console.log(example);
- window.exampleObject = example; // is still undefined in other js file cause that runs before this finishes.
- });
- window.exampleObject = example; // is undefined cause json fetch finishes after setting this.
- });
- class ExampleObject{
- constructor(settings){
- this.width = settings.width;
- this.height = settings.height;
- }
- setWidth(width){
- this.width = width;
- }
- setHeight(height){
- this.height = height;
- }
- }
- {
- "width": 3,
- "height": 4
- }
- $(document).ready(function () {
- // get object from global object
- console.log(window.exampleObject);
- // cant run this line since window.exampleObject is undefined.
- window.exampleObject.setWidth(6);
- console.log(window.exampleObject);
- });
Add Comment
Please, Sign In to add comment