How to Create a method inside A method in javascript ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to Create a method inside A method in javascript ?

I have a Javascript class "Command" , in this class I have two methods/functions I) contructor II) show. I want to create a method inside show , "previous" that returns a value x. I want to access this like.... var cmd = new Command; cmd.show().previous() , how can I do that ?

17th May 2021, 3:23 AM
Vaibhav Pawar
Vaibhav Pawar - avatar
5 Answers
+ 4
this is how to do it class cmd { show() { return { previous: () => { ... }; } } };
17th May 2021, 4:56 AM
Nima
+ 2
Something like this? function func() { alert("hello") func.display = function() { alert("world") }; } func() func.display()
17th May 2021, 4:52 AM
Rohit
+ 2
NimaPoshtiban thank you so much for your help
17th May 2021, 5:00 AM
Vaibhav Pawar
Vaibhav Pawar - avatar
+ 2
rkk thanks you
17th May 2021, 5:00 AM
Vaibhav Pawar
Vaibhav Pawar - avatar
0
rkk I want to create a function inside fun.display
17th May 2021, 4:56 AM
Vaibhav Pawar
Vaibhav Pawar - avatar