CLooker

Flyweight factory with cloning (credit Eric Elliott)

Dec 17th, 2017
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var carPrototype = {
  2.     gas: function gas(amount) {
  3.       amount = amount || 10;
  4.       this.mph += amount;
  5.       return this;
  6.     },
  7.     brake: function brake(amount) {
  8.       amount = amount || 10;
  9.       this.mph = ((this.mph - amount) < 0)? 0
  10.         : this.mph - amount;
  11.       return this;
  12.     },
  13.     color: 'pink',
  14.     direction: 0,
  15.     mph: 0
  16.   },
  17.  
  18.   car = function car(options) {
  19.     return extend(Object.create(carPrototype), options);
  20.   },
  21.  
  22.   myCar = car({
  23.     color: 'red'
  24.   });
  25.  
  26. test('Flyweight factory with cloning', function () {
  27.   ok(Object.getPrototypeOf(myCar).gas,
  28.     'Prototype methods are shared.'
  29.   );
  30. });
Add Comment
Please, Sign In to add comment