Zebestov Posted December 26, 2013 Share Posted December 26, 2013 Hi!I create some Class (for example GeomUtils) which contains static methods only. Can I define it as Object instead of function? Quote Link to comment Share on other sites More sharing options...
sbat Posted December 26, 2013 Share Posted December 26, 2013 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. Quote Link to comment Share on other sites More sharing options...
Zebestov Posted December 26, 2013 Author Share Posted December 26, 2013 Ok. Thank you for the tip about module pattern.I understand that private methods should be called like this:myPrivateMethod.call(this, "arg1", …);otherwise it will not have access to public and inherited methods? Quote Link to comment Share on other sites More sharing options...
sbat Posted December 26, 2013 Share Posted December 26, 2013 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/ Quote Link to comment Share on other sites More sharing options...
Zebestov Posted December 26, 2013 Author Share Posted December 26, 2013 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.