Note that there are some explanatory texts on larger screens.

plurals
  1. POJavascript object properties access functions in parent constructor?
    primarykey
    data
    text
    <p>So I'm using this pretty standard jquery plugin pattern whereby you can grab an api after applying the jquery function to a specific instance.</p> <p>This API is essentially a javascript object with a bunch of methods and data.</p> <p>So I wanted to essentially create some private internal methods for the object only to manipulate data etc, which just doesn't need to be available as part of the API.</p> <p>So I tried this:</p> <pre><code>// API returned with new $.TranslationUI(options, container) $.TranslationUI = function (options, container) { // private function? function monkey(){ console.log("blah blah blah"); } // extend the default settings with the options object passed this.settings = $.extend({},$.TranslationUI.defaultSettings,options); // set a reference for the container dom element this.container = container; // call the init function this.init(); }; </code></pre> <p>The problem I'm running into is that init can't call that function "monkey". I'm not understanding the explanation behind why it can't. Is it because init is a prototype method?($.TranslationUI's prototype is extended with a bunch of methods including init elsewhere in the code)</p> <pre><code>$.extend($.TranslationUI, { prototype: { init : function(){ // doesn't work monkey(); // editing flag this.editing = false; // init event delegates here for // languagepicker $(this.settings.languageSelector, this.container).bind("click", {self: this}, this.selectLanguage); } } }); </code></pre> <p>Any explanations would be helpful. Would love other thoughts on creating private methods with this model too.</p> <p>These particular functions don't HAVE to be in prototype, and I don't NEED private methods protected from being used externally, but I want to know how should I have that requirement in the future.</p> <p>// Edited based on Matthew's comment So I tried moving the prototype definition based on Matthew's comment. This seems to work now, but still not sure if this is the correct way to be doing this. Thoughts? Obviously it would be cleaner if I move the prototype object into a separate area</p> <pre><code>$.TranslationUI = function (options, container) { function monkey(){ console.log("blah blah blah"); } // extend the default settings with the options object passed this.settings = $.extend({},$.TranslationUI.defaultSettings,options); // set a reference for the container dom element this.container = container; $.extend($.TranslationUI.prototype, { init : function(){ monkey(); // editing flag this.editing = false; // init event delegates here for // languagepicker $(this.settings.languageSelector, this.container).bind("click", {self: this}, this.selectLanguage); } } ); // call the init function this.init(); }; </code></pre> <p>So while this works, the crappy part is that I'm re-initing prototype every time that constructor runs. I'm sure that's not efficient. But not sure how else to have the prototype methods have access to private functions/variables of a certain instance.</p>
    singulars
    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.
 

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