Did you mean chaining?
var Geometry = function(type) { this._type = type; this._color = 'transparent'; this._size = 1; this._rotation = 0; this._scale = 1; }; Geometry.prototype = { constructor: Geometry, setColor: function(color) { this._color = color; return this; }, setSize: function(size) { this._size = size; return this; }, setRotation: function(rotation) { this._rotation = rotation; return this; }, setScale: function(scale) { this._scale = scale; return this; }, setParams: function(settingObject) { this.setColor(settingObject.color); this.setSize(settingObject.size); this.setRotation(settingObject.rotation); this.setScale(settingObject.scale); return this; }, toString: function() { return 'This is ' + this._type + ': color=' + this._color + ' size=' + this._size + ' rotation=' + this._rotation + ' scale=' + this._scale; } } var Draw = function() {}; Draw.prototype = { constructor: Draw, cube: function() { return new Geometry('cube') }, circle: function() { return new Geometry('circle') }, rect: function() { return new Geometry('rect') } } var draw = new Draw(); var cube1 = draw.cube().setColor('red').setRotation('20deg').setSize(2).setScale(0.25); var cube2 = draw.cube().setParams({ color: 'red', rotation: '20deg', size: 2, scale: 0.25 }); console.log(cube1.toString()); console.log(cube2.toString());
Or an additional method to simplify property assignment
var setParams = function(geometry, settingObject) { geometry .setColor(settingObject.color) .setSize(settingObject.size) .setRotation(settingObject.rotation) .setScale(settingObject.scale); }; var Geometry = function(type) { this._type = type; this._color = 'transparent'; this._size = 1; this._rotation = 0; this._scale = 1; }; Geometry.prototype = { constructor: Geometry, setColor: function(color) { this._color = color; return this; }, setSize: function(size) { this._size = size; return this; }, setRotation: function(rotation) { this._rotation = rotation; return this; }, setScale: function(scale) { this._scale = scale; return this; }, toString: function() { return 'This is ' + this._type + ': color=' + this._color + ' size=' + this._size + ' rotation=' + this._rotation + ' scale=' + this._scale; } } var Draw = function() {}; Draw.prototype = { constructor: Draw, cube: function() { return new Geometry('cube') }, circle: function() { return new Geometry('circle') }, rect: function() { return new Geometry('rect') } } var draw = new Draw(); var cube1 = draw.cube().setColor('red').setRotation('20deg').setSize(2).setScale(0.25); var cube2 = draw.cube(); setParams(cube2, { color: 'red', rotation: '20deg', size: 2, scale: 0.25 }); console.log(cube1.toString()); console.log(cube2.toString());
Or add a method to the prototype
Geometry.prototype.setParams = function(settingObject) { this.setColor(settingObject.color) .setSize(settingObject.size) .setRotation(settingObject.rotation) .setScale(settingObject.scale); return this; };
Or inherit a class and extend its functionality.
var GeometryExt = function() { Geometry.apply(this, arguments); }; GeometryExt.prototype = Object.create(Geometry.prototype); GeometryExt.prototype.setParams = function(settingObject) { this.setColor(settingObject.color) .setSize(settingObject.size) .setRotation(settingObject.rotation) .setScale(settingObject.scale); return this; }; var cubeExt = new GeometryExt('Mega-cube'); cubeExt.setParams({ color: 'red', rotation: '20deg', size: 2, scale: 0.25 });