If you want to create a basic "template" for a group of objects, you can do it like this:
function exampleObject() {
this.property = arguments[0];
};
In the above code snippet, the "template" (exampleObject
) is simply a JavaScript function that takes in N arguments (although other options are possible).
To create objects based on this template, all you need to do is instantiate them as shown below:
var obj1 = new exampleObject('value1');
Check out the example code snippet below for a demonstration.
function exampleObject() {
this.property = arguments[0];
};
var obj1 = new exampleObject('value1');
var obj2 = new exampleObject('value2');
var obj3 = new exampleObject('value3');
console.log(obj1.property);
console.log(obj2.property);
console.log(obj3.property);