javascript - Getting function from within object function -


i've been learning oop , playing around in javascript, , wondering how looks work...

function myapp(){     this.nav = function(){        function toggle(state = 'show'){           //toggle nav code...       }     toggle();     }   }  var app = new myapp(); app.nav(); 

how go accessing toggle function here this...

app.nav().toggle('hide'); 

you need return this.
here example:

function myapp(){     this.nav = function(){     this.toggle = function(state = 'show'){       console.log(state);           }        this.toggle();     return this;   } }  const app = new myapp(); app.nav(); // show app.nav().toggle('hide'); // show hide 

also need attach function object (this.toggle).
hope help.


Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -