Is there such thing as a class method in Javascript?
Example of class method in Ruby
class Item
def self.show
puts "Class method show invoked"
end
end
Item.show
Like so
function Item() {
// you can put show method into function
// Item.show = function () {};
}
Item.show = function () {
console.log('Class method show invoked');
}
Item.show();
But better use object literal
var Item = {
show: function () {
console.log('Class method show invoked');
}
};
Item.show();
There's many ways to do this, but my personal favorite is:
function Person(name) { // This is the constructor
this.name = name;
this.alive = true;
}
Person.prototype.getName = function() {
return this.name;
}
myHuman = new Person('Bob')
console.log(myHuman.getName())
class"method" per se, but check out ecmascript6's use of the actualclassdeclaration: github.com/lukehoban/es6features#classes