0

Why this does not work ? According to JQuery Document Ready - Multiple allowed? it should. It throws a "jQuery.Deferred exception: Dog is not defined ReferenceError: Dog is not defined"

$(function() {
  class Dog {
    constructor() {
    }
    bark(){
        console.log('bark')
    }
}
})


$(function() {
  var teckel = new Dog()
  teckel.bark()
})
Yvon Huynh
  • 453
  • 3
  • 16

1 Answers1

0

Thats for the functional scope in JS. Here you define the class inside a function(first ready function), so its available only inside that function. If you want to make it global, try moving it outside the ready functions or add it to the window object(like below).

$(function() {
  class Dog {
    constructor() {
    }
    bark(){
        console.log('bark')
    }
}
  window.Dog = Dog;
})


$(function() {
  var teckel = new Dog()
  teckel.bark()
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Vishnu
  • 897
  • 6
  • 13