Fix bug in Javascript involving chained methods not checking whether object is null