JavaScript Method overload in JavaScript method overloading javascript,method overload js

Consider sample html code:

<head>
    ....
    <script src="/js/MyClass.js"></script>
    <script src="/js/extend.js"></script>
    <script>
        $(document).ready(function($) {
            var $obj=new MyClass();
            $obj.run();
        });
    </script>
</head>
<body>

MyClass.js file:

var MyClass=function()
{
    this.run=function() {
        alert("MyClass");
    }
}

extend.js file:

MyClass.prototype.run=function() {
    alert("Extend");
}

Why does this code alert "MyClass" instead of "Extend"? How to correctly rewrite (overload) class method?

Answer:1

The prototype is only used when the object itself does not have the property already.

In your case, you put run on the object itself, so that is what run will resolve to.

In a way, your MyClass is overriding its own prototype.

Maybe you want

var MyClass=function(){};
MyClass.prototype.run = function() {
    alert("MyClass");
}

var overridden = new MyClass();
overridden.run = function() {
     alert("Extend");
}

That would be an instance with a different method than its prototype.

Or you could chain prototypes to simulate a class hierarchy.

Answer:2

In the following code, I have two Book Collections. One collection has "Moby Dick," while the other collection has "The Firm." However, when I look at the second collection, it also has Moby Dick. How ...

In the following code, I have two Book Collections. One collection has "Moby Dick," while the other collection has "The Firm." However, when I look at the second collection, it also has Moby Dick. How ...

Check here javascript:Function("alert('this is ok')")(); same as new Function("alert('this is ok')")(); What the syntax called ? Any reference ? EDIT Sorry, my fault,it's only a label,and it's ...

Check here javascript:Function("alert('this is ok')")(); same as new Function("alert('this is ok')")(); What the syntax called ? Any reference ? EDIT Sorry, my fault,it's only a label,and it's ...

  1. what does javascript do
  2. what does javascript mean
  3. what does javascript look like
  4. what does javascript void mean
  5. what does javascript need to work on a website
  6. what does javascript compile to
  7. what does javascript stand for
  8. what does javascript void(0)
  9. what does javascript code look like
  10. what does javascript do for a website
  11. what does javascript do in html
  12. what does javascript run on
  13. what does javascript error mean
  14. what does javascript eval do
  15. what does javascript map return
  16. what does javascript developer do
  17. what does javascript require do
  18. what does javascript trim do
  19. what does javascript await do
  20. what does javascript have to do with java

I am designing a game in HTML5 with Canvas/JavaScript. My aim is to create a timer that will countdown from a certain amount of minutes down to zero, during the time you need to shoot as many ...

I am designing a game in HTML5 with Canvas/JavaScript. My aim is to create a timer that will countdown from a certain amount of minutes down to zero, during the time you need to shoot as many ...

  1. create timer javascript
  2. creating timer js
  3. create countdown timer javascript
  4. create timer using javascript
  5. create simple timer javascript
  6. create timer event javascript

Hello guys I have some problem with my website. Here's the situation: Page 1 <script type="text/javascript" src="jscript.js"> <input type="button" value="change" onClick="changeAttr()"> ...

Hello guys I have some problem with my website. Here's the situation: Page 1 <script type="text/javascript" src="jscript.js"> <input type="button" value="change" onClick="changeAttr()"> ...

  1. change element attribute javascript
  2. change element attribute jquery
  3. change element attribute microstation
  4. change element attribute selenium
  5. change element attribute value jquery
  6. change element attribute
  7. change element attribute css
  8. change element attribute angular
  9. change element attribute angularjs
  10. change element attribute selenium python
  11. change element attribute html
  12. js change element attribute
  13. change html element attribute javascript
  14. vue change element attribute
  15. react change element attribute
  16. js change element attribute value
  17. puppeteer change element attribute
  18. vuejs change element attribute
  19. protractor change element attribute
  20. change dom element attribute