Hacker News new | past | comments | ask | show | jobs | submit login

Is there a a reason to learn bind, where I can use arrow function, which is cleaner and more readable? (And probably does bind under the hood)

Also, what is the reason for the prototype style inheritance? What is the benefit? Why use it over "classic", java-style OOP?




> Is there a a reason to learn bind, where I can use arrow function, which is cleaner and more readable? (And probably does bind under the hood)

Off the top of my head, arrow functions come in handy when you're defining an "instance method" that you want to use as an event handler; you want `this` to refer to the instance, not the event. That's 95% of the time you'd use an explicit call to `bind` anyhow.

Another case to use `bind` would be a generic function that isn't specific to the type of object you're acting upon. I wrote up a short example on jsfiddle: https://jsfiddle.net/LyndsySimon/5zqtjd82/

You can also use binding to create decorators that work on instance methods: https://jsfiddle.net/LyndsySimon/n57or0u5/


In prototype inheritance, objects inherit from objects. This creates flexibility, since you can dynamically create a base object to inherit from. You can also easily augment base objects after they're inherited from. Class-based inheritance is less powerful in most languages due to the increased runtime rigidity of the class hierarchy. That's the theory. In practice everyone treats prototypes as classes.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: