P
P
psyhO_octopus2014-10-30 14:39:16
JavaScript
psyhO_octopus, 2014-10-30 14:39:16

How to properly use OOP in JavaScript?

Hello.
The more I write code, the more I dislike it. I would like to write normal OOP code. I have a good understanding of how prototypes, inheritance, etc. work, but I don't know how to apply it when I create interfaces that work with the DOM.
Most of the books and articles I've seen describe only concepts and patterns without being tied to real-life interface development tasks.
Advise where to look, what to read.
PS Only frameworks do not advise, please.)

Answer the question

In order to leave comments, you need to log in

7 answer(s)
U
UbuRus, 2014-10-30
@UbuRus

JavaScript doesn't need OOP.
Use modularity: largescalejs.ru
Read:
habrahabr.ru/post/240219

I
Itvanya, 2014-10-30
@Itvanya

Learn to write modules, to separate open and closed pieces of code for an object created through a constructor with a closure. You can get good at writing OOP in JavaScript by learning how to write good animations.

K
Konstantin Kitmanov, 2014-10-30
@k12th

Working with the DOM must be encapsulated in either templating or two-way binding. But when you write a template engine and ViewModel, then OOP will come in handy.
OOP also comes in handy in complex business logic.

A
aush, 2014-10-30
@aush

https://ru.wikipedia.org/wiki/TypeScript
http://www.typescriptlang.org

P
psyhO_octopus, 2014-10-30
@psyhO_octopus

Here's what I meant. Let's say I'm making some kind of panel. There are buttons on the panel. For some events, buttons can be in the loading or disabled state, for example. You can make a Panel class and when some event occurs, find a button and set a class for the loading state, for example. And you can make another Button class that has a method for changing states. Inside the Panel, write something like var successBtn = new Button(...) and already work with the button not as a node, but as an object.
The example is simple, of course, but I think it reveals the essence. Those. I want to learn how to parse the interface into parts and work with them as with objects, and not create one big class and write noodles for working with the DOM there

S
Sergey Romanov, 2014-10-30
@Serhioromano

PS Only frameworks do not advise, please.)

There is no way without a framework. If you don't want to use anyone else's framework, you'll have to at least create a small one of your own. Because that's how your problem is solved. In javascript, it is very inconvenient to organize the code if you use pure javascript. But frameworks solve this problem. You take out all the ugly logic in the framework, connect it, and write your code in beautiful code.
For example, this is how Microsoft solved this problem. Javascript based applications use cat methods to create classes and call them.
So either find something good where a lot of mind and effort has been invested, like Angular, for example, or write something of your own.
But it is IMPOSSIBLE to write beautiful OOP code in bare Javascript.

A
Alexander Kaloshin, 2014-10-31
@undassa

See how the creation of a class from CoffeeScript is implemented in JS.
Use prototype
And yes, there is no point in even thinking about frameworks.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question