📜 ⬆️ ⬇️

Javascript blocks

Hi, Habrachitel!

At this late hour, one very strange thought came to my head. Without hesitation, I decided to write a short post about it.
Well, I still don’t have the time and effort to write the second article about WebRTC , but for a post note I decided to find brain and temporary resources.

The essence of the thought, or even the question - why no one (I have met) uses separate blocks of code (besides those used with if else expressions, etc.) in their JavaScript applications?

How to create a block of code in javascript?


Easy! {} at the beginning of an expression is considered a block (and not an object). For example:
{ console.log('Hello from a block'); } 

How can a block be useful?


In JavaScript, code blocks do not create their own scope (skoup), so we will look for utility in something else.
The most elementary use is the separation of logical parts of code. Yes, what! The block can be used with a label.
 MyBlock: { console.log('This is MyBlock'); } 

Sense from it in this situation is not enough. With such success, you can replace the label with a comment, but how beautiful it looks.
But the problem remains in the limited label.
')
An experienced and not very programmer can say that all logical blocks / parts of the code are better placed in separate functions. But I will try to disagree immediately. Let's look at a life example?
Suppose I have a Backbone app. When initializing each view, I have to perform standard actions: subscribe to global events, subscribe to changes in a model or collection, etc.
 Backbone.View.extend({ initialize: function () { this.model.on('change', this.render); this.model.on('add', this.onAdd); this.model.on('change:type', this.onResetType); app.subscribe('reload', this.render, this); app.subscribe('remove', this.remove, this); app.subscribe('vodka', this.drink, this); } }); 

For some reason I do not want to make it into separate functions. I'm used to the fact that in the initialize method I can immediately see a subscription to all the necessary events.
So why shouldn't I divide this mass of code into separate logical blocks of code in the framework of one method and scop?
 Backbone.View.extend({ initialize: function () { ModelEvents: { this.model.on('change', this.render); this.model.on('add', this.onAdd); this.model.on('change:type', this.onResetType); } AppEvents: { app.subscribe('reload', this.render, this); app.subscribe('remove', this.remove, this); app.subscribe('vodka', this.drink, this); } } }); 


disadvantages


I do not even know what the disadvantages of this method of code separation can be. Perhaps there is a possibility that it can be confused - to confuse a block of code with an object. But I have no such inclination.

Conclusion: to use such blocks is a matter of everyone’s choice. Comments, blocks - as you like. But I would still like to find an interesting use for such blocks.

Thank you all for your attention.

Source: https://habr.com/ru/post/176951/


All Articles