Skip to main content
gamedev.net
🔒 Locked

[web] Javascript: Embedded 'classes'?

Started by deadimp Jan 5, 2007 at 8:13 PM 1 replies 750+ views
Original Post
deadimp
deadimp
Is there a way to do embedded 'classes' in &#106avascript? (JScript, JS, whatever's the correct name for it) I've just recently figured out how to do 'classes' in &#106avascript, which is kind of odd, and now I'm wondering how to make embedded classes. My attempt: <!--STARTSCRIPT--><!--source lang="javascript"--><div class="source"><pre> <span class="cpp-keyword">function</span> <span class="cpp-keyword">Class</span>() { <span class="cpp-keyword">this</span>.Func=<span class="cpp-keyword">function</span>() ... <span class="cpp-comment">//Priviledged</span> <span class="cpp-keyword">Class</span>.Func=<span class="cpp-keyword">function</span>() ... <span class="cpp-comment">//Static</span> <span class="cpp-keyword">Class</span>.Sub=<span class="cpp-keyword">function</span>() { <span class="cpp-comment">//Static...</span> <span class="cpp-keyword">Class</span>.Sub.Func=<span class="cpp-keyword">function</span>() { <span class="cpp-comment">/* Error here */</span> } } } </pre></div><!--ENDSCRIPT--> I'm using Firefox 2.0.0.1
Projects:> Thacmus - CMS (PHP 5, MySQL)Paused:> dgi> MegaMan X Crossfire
WalterTamboer
WalterTamboer
The term is &#106avascript or JS when it's an abbreviation. JScript is something else :)


Let me give you an example.

/** * A class in JavaScript is just a function. */function MyClass(){}/** * A public var with value 12 */MyClass.prototype.MyVar = 12;/** * A public function */MyClass.prototype.MyFunction = function(){	alert('Hi there!');};// Now create the inner class/** * The second class! */MyClass.MySecondClass = function(){}/** * A public var with value 891273 */MyClass.MySecondClass.prototype.MyVar = 891273;



(Edit: http://www.sitepoint.com/article/oriented-programming-1 is pretty good intro to OOP in &#106avascript, <a href="http://dean.edwards.name/weblog/2006/03/base/">http://dean.edwards.name/weblog/2006/03/base/</a> for 'real' OOP!)<br>(Edit 2: Create actual url's of my first edit ;))<br>(Edit 3: I can't work with BB code, hope this is my last edit :P)
deadimp
deadimp
Thanks for the info!

... Figured out what I was doing wrong in my example. I was embedding the sub-class, therefore its type would be instantiated until I called the constructor of the containing class. I forgot to include where I called the function "Class.Sub.Func". Yeah...
Sorry to say, but I hate &#106avascript.<br>*Sobs and crawls slowly back to C++*
Projects:> Thacmus - CMS (PHP 5, MySQL)Paused:> dgi> MegaMan X Crossfire

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.