Is it possible to use OO approach in Java Script?
I use JavaScript in both server side and client side using node.js.Currently i am using queries to CRUD operations instead of queries is it possible to use DTO’S to save data in database?
Is it possible to use OO approach in Java Script? I use JavaScript in
Share
Yes, you can emulate it using prototypical inheritance. The rules are quite different since the language is prototype driven and you’ll need to do some research regarding prototype chains and such, but in the end it turns out quite useful.
Check out things in Node core which inherit from EventEmitter. They have a built in function called util.inherits, which has an implementation for later versions of ECMA. It looks like this:
An example use is the Stream class:
https://github.com/joyent/node/blob/master/lib/stream.js#L22-29
In coffeescript, classes compile to a slightly different set of code which boils down to this __extends function. I believe this is going to have more cross browser compatibility, but I don’t specifically recall who doesn’t support Object.create.