Creating classes
To create a classA type of object. is as simple as creating a function with the name of the class. The conventionA consistent style used to make code more readable to humans. is to capitalize the name of the class.
function ClassName(propOne, propTwo) {
this.propOne = propOne;
this.propTwo = propTwo;
this.someMethod = function(someValue) {
this.propOne = someValue;
}
}
The term this
is used to refer to the specific object in question. Methods can be added inside the function definition, again using this
to refer to the object.
After a class has been created, an object can be created using the syntax
var newObject = new ClassName(valueOne, valueTwo);
or
var newObject = new ClassName();
newObject.propOne = valOne;
newObject.propTwo = valTwo;
and the method can be called using the syntax
newObject.someMethod(someValue);