addyosmani / essential-js-design-patterns

Repo for my 'Learning JavaScript Design Patterns' book
http://addyosmani.com/resources/essentialjsdesignpatterns/book/
4.8k stars 791 forks source link

Factory pattern code example bug removal #227

Open omamazainab opened 3 years ago

omamazainab commented 3 years ago

Expected Behaviour

Since the default values were set for the factory method i.e, Car constructor, if user did not pass any arguments at all i.e,

var carFactory = new VehicleFactory();
var car = carFactory.createVehicle();

it should create a Car object with default values. which would result in this object

Car { doors: 6, state: 'brand new', color: 'yellow' }

but instead it throws an error because if nothing is passed options will be undefined.

Correction

we set the default value of options to an empty object. It will then allow assigning all of the default values