× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

Using "this" keyword inside method of an object

Mehran Hatami
May 02, 2015
<p>You could have 2 different class like functions here:</p> <p>‍‍‍‍‍‍‍‍‍‍‍</p> <pre><code>var Fun = function(){ }; Fun.prototype.f1 = function(){ console.log ( "Help FUN"); return this; }; Fun.prototype.f2 = function(){ console.log ( "Print FUN"); return this; }; </code></pre> <p> </p> <p>Then define a property of <code>Fun</code> in your <code>Main</code>:</p> <p> </p> <pre><code>var Main = function(){ }; Main.prototype.fun = new Fun(); </code></pre> <p> </p> <p>or like:  </p> <pre><code>var Main = function(){ this.fun = new Fun(); }; </code></pre> <p>Then you can use it like:</p> <pre><code>var main = new Main(); main.fun.f1().f2(); </code></pre> <p>or</p> <pre><code>main.fun.f2().f1(); </code></pre> <p>This tip was originally posted on <a href="http://stackoverflow.com/questions/23732294/Using%20%22this%22%20keyword%20inside%20method%20of%20an%20object/23732435">Stack Overflow</a>.</p>

Get New Tutorials Delivered to Your Inbox

New tutorials will be sent to your Inbox once a week.

comments powered by Disqus