Adding listener functions to a JavaScript object
问题 I have the following code which defines a Car . Each Car has a color, along with a setColor(color) function. I want to add listener functions which are called whenever setColor(color) is called, and I want to be able to tack these listener functions on whenever I want. Is this a suitable approach? Is there a cleaner way? function Car() { this._color = 'red'; this._callbacks = {}; this.setColor = function(color) { this._color = color; console.log(">>> set car color to " + color); if (this.