ref – https://abdulapopoola.com/2013/03/30/static-and-instance-methods-in-javascript/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
var Person = function (name, age){ //private properties var priv = {}; //Public properties this.name = name; this.age = age; //Public methods this.sayHi = function(){ alert('hello'); } } // A static method; this method only // exists on the class and doesn't exist // on child objects Person.sayName = function() { alert("I am a Person object ;)"); }; // An instance method; // All Person objects will have this method Person.prototype.setName = function(nameIn) { this.name = nameIn; } // Tests var per = new Person('John Doe', 22); //Shows alert Person.sayName(); //TypeError: Object [object Object] has no method 'sayName' per.sayName() //Show alert per.sayHi(); //John Doe per.name; //22 per.age; per.setName('Jane Doe'); //Jane Doe per.name; |