Return to Snippet

Revision: 27869
at June 25, 2010 10:31 by geekyjohn


Initial Code
/** 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"

Initial URL


Initial Description
Simple template for setting up classes in JS

Initial Title
Static and Instantiable Classes in JavaScript

Initial Tags
javascript, class, template

Initial Language
JavaScript