Note that there are some explanatory texts on larger screens.

plurals
  1. POJavascript OOP inheritance not working
    primarykey
    data
    text
    <p>So I am writing something using augment for inheritance and for some reason I can run <code>this.setButtons(type)</code> and <code>console.log(this.buttons)</code> in that method, but when I run my <code>this.getButtons()</code> it comes back as undefined, even though <code>getButtons</code> just returns <code>this.buttons</code>. Any help would be greately appreciated. I will post up all the code I have so far, because maybe I'm not inheriting properly. Thank you in advance.</p> <pre><code>var ContextMixin = function () {}; ContextMixin.prototype = { createElements: function (el, mode, type) { var m; if (mode == 'exact') { $("#" + el).append("&lt;ul id='contextmenu'&gt;"); } else { $(el).each(function () { m = $(this).append("&lt;ul id='contextmenu'&gt;"); }); $('body').append(m); } $("#contextmenu").css({ 'position': 'absolute', top: 13, left: 13 }); var new_buttons = this.getButtons(); $.each(this.buttons['buttons'], function () { m.append("&lt;li id='" + this + "'&gt;" + this + "&lt;/li&gt;"); }); }, attachEvents: function () { functions = this.getFunctions(type); buttons = this.getButtons(); for (index in buttons['buttons']) { addEvent(buttons['buttons'][index], this.functions[index][0], this.functions[index][1]); }; }, setFunctions: function (type) { var callback = { success: function (msg) { this.functions = msg; }, failure: function () { alert('Error getting functions') } }; $.ajax({ type: 'GET', url: 'function_list.php?type=' + type, success: function (msg) { this.functions = msg; } }); }, getFunctions: function () { return this.functions; }, setType: function (value) { this.type = value; }, getType: function () { return this.type; }, setButtons: function (type) { $.ajax({ type: 'GET', url: 'button_list.php?type=' + type, success: function (reply) { this.buttons = reply; } }); }, getButtons: function () { return this.buttons; } } function createMenu(el, type, mode) { this.setButtons(type); this.setFunctions(type); this.createElements(el, mode, type); } augment(createMenu, ContextMixin); function augment(receivingClass, givingClass) { if (arguments[2]) { //Only give certain methods. for (var i = 2, len = arguments.length; i &lt; len; i++) { receivingClass.prototype[arguments[i]] = givingClass.prototype[arguments[i]]; } } else { //Give all methods for (methodName in givingClass.prototype) { if (!receivingClass.prototype[methodName]) { receivingClass.prototype[methodName] = givingClass.prototype[methodName]; } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload