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
Post a Comment