Y
Y
Yaroslav2015-01-14 19:50:19
JavaScript
Yaroslav, 2015-01-14 19:50:19

How correct are the listed architectural decisions in JS?

I am creating a website , client side in HTML5/JS/CSS. Server - Java / Spring. Normally I understand server programming, but in the client part - I'm very poorly oriented.
So the questions are:
1) Is it worth using OOP in JS? If so, why is it so poorly implemented there? If not, then how can I achieve a good level of abstraction, I'm used to working with objects everywhere.
2) Should I use getters/setters for JS classes?
Consider a small piece of source code:

//Файл section.js, подключается в <head> страницы 
function Section(id) {
    var _id = id;

    this.paste = function () {
        ...
    }
}

//Внутри страницы
<script>
var thisSection = new Section("${section.id}");
</script>
...
<button onClick="thisSection.paste()">Вставить</button>

The questions are:
3) Is it worth creating a separate file for each js class? If I have 10-15 js files connected on the page - is it good?
4) Will it be the right decision to stuff several small classes into one js file?
5) Is the approach correct - to prescribe the onClick + function, or is it better to prescribe the id and then, through jQuery, after loading the page, find all the necessary elements by id and prescribe clicks, such as $("#id").click(...)?
6) The whole page is divided into blocks (in my case, these are JSP tags 2.0) with parameters. It seems to me convenient to prescribe script tags next to HTML in these blocks and logic in them already. Is this approach correct?
7) They say it's better to separate logic and presentation, but if I separate them - how do I do var thisSection = new Section("${section.id}"), i.e. how to create a variable bound to a specific block of HTML markup, but without declaring it inside this block?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Taratin, 2015-01-14
@kciray


1) Should you use OOP in JS? If so, why is it so poorly implemented there? If not, then how can I achieve a good level of abstraction, I'm used to working with objects everywhere.
Use coffeescript there oops more familiar
2) Should I use getters/setters for JS classes?
See support
https://developer.mozilla.org/en/docs/Web/JavaScri...
The second will be better, because then it will be hard to redo the logic and you won’t hang several handlers.
3) Is it worth creating a separate file for each js class? If I have 10-15 js files connected on the page - is it good?
At the development stage, it's fine, throw the merged and minified version into production
Absolutely incorrect (you have separate templates and code on the server - on the client, it’s similar to sculpt everything in a heap)
Here's something really weird

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question