Jump to content

Class with static methods


Zebestov
 Share

Recommended Posts

In JavaScript there are no classes. :) 

 

So if you mean something like:  

MyModule = {}MyModule.testFunction = function() {}

Then yes, it would work.

 

You may want to google for "module" pattern for more robust implementation in JavaScript (for example, you'll likely want to have private properties/functions as well. 

Link to comment
Share on other sites

There are no private and inherited methods in JavaScript either, so it is hard to answer these question precisely. Prototype-based OOP is just different. 

 

To model private functions of the module you would generally use function scope:

MyModule = {};(function () {    MyModule.testFunction = function() {        myPrivateFunction();    }        function myPrivateFunction() {        alert("Private")    }})();MyModule.testFunction();

http://jsfiddle.net/Cfp6V/

 

Link to comment
Share on other sites

I know that it's actually not classes and inheritance. I'm just looking realization of similar model in javascript. :)

At the moment I can't find nothing about emulation private instance variables. Examples I found is all about some kind of private static variable one for all instances.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...