1

Is it possible to call another prototype method inside a prototype method ? Like below.

jQuery(document).ready(function ($) {
 let gui = new GUI();
 let App = new App(gui);
});
var App = function(gui) {
 this.gui = gui;
 this.init();
 return this;
};
App.prototype.init = function() {
 this.gui.test();
};
var GUI = function() {
 return this;
};
GUI.prototype.test = function() {
 console.log("Test");
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

I would like to call something like this.

Best regards and thx for ur help

Kevin Boucher
16.8k3 gold badges51 silver badges57 bronze badges
asked May 29, 2018 at 19:37
1
  • 1
    @KevinBoucher Since test is a method on this.gui, this.gui.test() is correct. Commented May 29, 2018 at 19:45

1 Answer 1

1

Yes, you certainly can. The only reason your code doesn't work is that you are shadowing App on the 3rd line.

Working code:

jQuery(document).ready(function ($) {
 let gui = new GUI();
 let app = new App(gui);
});
var App = function(gui) {
 this.gui = gui;
 this.init();
 return this;
};
App.prototype.init = function() {
 this.gui.test();
};
var GUI = function() {
 return this;
};
GUI.prototype.test = function() {
 console.log("Test");
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

answered May 29, 2018 at 19:44
Sign up to request clarification or add additional context in comments.

1 Comment

Lower case a in app: let app = new App(gui);

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.