0

When I read JavaScript The Definitive Guide, section 9.3, I encountered this:

Class methods
These are methods that are associated with the class rather than with instances.

How do I implement class methods in JavaScript?

6
  • If, by "class" you mean constructor function, then yes, just define methods on the constructor. Commented Dec 11, 2012 at 15:38
  • So, what does the term "class" refer to? Commented Dec 11, 2012 at 15:38
  • @Šime Vidas: Not on the constructor, on the constructor prototype. Commented Dec 11, 2012 at 15:39
  • Probably a duplicate of this question: stackoverflow.com/questions/4305578/… Commented Dec 11, 2012 at 15:39
  • @RyanLynch Methods defined on the constructor's prototype are not class methods, but instance methods. OP is asking about class methods, not instance methods. Commented Dec 11, 2012 at 15:40

1 Answer 1

4

You can just create a function as a property of another function:

function MyClass() {
}

MyClass.someFunction = function() { };
Sign up to request clarification or add additional context in comments.

1 Comment

Since JavaScript currently doesn't have a true OOP model the above is the equivalent of a static or class method, static or class properties can be defined in a similar fashon MyClass.someProperty = "myValue"; On a similar note instance methods are defined on the prototype property of objects so MyClass.prototype.someFunction = function() { }; is an instance method and MyClass.prototype.SomeProperty = "someValue"; is an instance property. These aren't the only ways to declare these types of functions and properties you can search for Object Oriented JavaScript to find more examples.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.