I am creating a library in javascript that create javascript objects.
- How can I write the interface of the library so that their users can create such objects both WITH and WITHOUT new? (I saw a lot of answers that propose constructors that auto-call itselfs with new if they were not invoked with new in first place but no the other way around).
- Can we use new with Object.create? For example: let dog = new Object.create(animal);
- How to provide inheritance
To illustrate with code, how do you write the functions Animal and Dog below so that the following expressions are valid:
let animal = new Animal(); // valid
let animal = Animal(); // valid also, we should return the same object
let dog = new Dog(); // valid, dog inherits/shares functions and properties from Animal.
let dog = Dog(); // valid also, same case as in previous call.
Thank you so much.
newwill have no effect. Perhaps look into that. Example.