3 ms·
Right. The "new" keyword is never necessary and is usually actively counterproductive. In Java, "new Hammer()" must allocate a new object and it must be exactly
by orangecat 15y ago
Right. The "new" keyword is never necessary and is usually actively counterproductive. In Java, "new Hammer()" must allocate a new object and it must be exactly a Hammer instance, and to work around those limitations we get the joy of AbstractHammerFactoryFactory. In Python, "Hammer()" can return an existing object and it can be a subclass of Hammer; callers don't need to know the details.
- strager 15y agoYes; just give me something; I don't care what it really is, as long as it works with how I use it (i.e. adheres to some interface).
- tomp 15y agoIn Java, it's a bit different. There, classes (`new Hammer()`) and function (`Hammer()`) are orthogonal concepts that cannot be mixed, while in JavaScript, they are one and the same thing. In addition, AFAIK there are some funny issues in JavaScript when a constructor function returns an object that is not the object bound to `this` (i.e. the new object created by the `new` keyword).
- MatthewPhillips 15y agonew can be useful. I believe instanceof only works with objects created with new.