salauddin salauddin - 4 months ago 10
Javascript Question

Uncaught TypeError: Cannot read property 'maximum' of undefined

<button id="max" onclick="Application(document).maximum();">Maximum</button>


While calling
maximum
I get this error:


Uncaught TypeError: Cannot read property 'maximum' of undefined


function Application(document) {
this.maximum=function () {
document.getElementById("result-value").innerHTML=new StatisticsCalculator().maximum([1,2]);
}
}


I included both(
Application,StatisticsCalculator)
js files html file.Still getting the problem.

How to resolve this?

Answer

Include new before Application() call

<script>
  function Application(args) {
    this.maximum = function() {
      console.log(args)
    }
  }
</script>
<button id="max" onclick="new Application('maximum called').maximum();">Maximum</button>

alternatively

<script>
  function Application(args) {
    this.maximum = function(args) {
      console.log(args)
    }
  }
  
  var app = new Application();
</script>
<button id="max" onclick="app.maximum('maximum called');">Maximum</button>

Comments