是否有可能在原型方法中调用另一个原型方法?就像下面。
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>
我想称之为这样的东西。
对你的帮助致以最良好的问候和感谢
发布于 2018-05-29 19:44:42
是的你当然可以。您的代码不能工作的唯一原因是您在第3行跟踪App
。
工作代码:
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>
https://stackoverflow.com/questions/50591725
复制相似问题