/ Published in: JavaScript
Simple template for setting up classes in JS
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
/** InstantiableClass */ var InstantiableClass = function(arg1, arg2) { this.aProperty = "value"; this.anotherProperty = "another value"; this.init = function(someArg, isAnotherArg) { //do some initializiation. if (isAnotherArg) { this.anotherProperty = someArg; } }; this.someMethod = function() { //do something var localVar = this.aProperty + " " + this.anotherProperty; return localVar; }; this.init(arg1, arg2); } var myObj = new InstantiableClass("Jumped over the lazy dog", true); myObj.aProperty = "The quick brown fox"; myObj.someMethod(); //will return "The quick brown fox Jumped over the lazy dog" var myNextObj = new InstantiableClass(null, false); myNextObj.aProperty = "Hello"; myNextObj.someMethod(); //will return "Hello another value" /** StaticClass */ var StaticClass = { aProperty: "value", anotherProperty: "another value", someMethod: function() { //do something var localVar = StaticClass.aProperty + " " + StaticClass.anotherProperty; return localVar; } }; StaticClass.aProperty = "Lazy dogs"; StaticClass.anotherProperty = "let foxes jump over them"; StaticClass.someMethod(); // will return "Lazy dogs let foxes jump over them"