📜 ⬆️ ⬇️

Abnormal Javascript

In this topic, I want to talk about unusual js constructions, as well as illustrating some examples related to objects and method calls that, using a non-trivial syntax, can cause questions for almost all beginners to use js.

The purpose of the topic (and immediately a disclaimer) is to help beginners not to fall into a catatonic stupor at the sight of something like
user[(os[((user.microsoft_adept ? microsoft : apple).system || "linux")].install_carma <= user.carma) ? "install" : "cant_install"](os[((user.microsoft_adept ? microsoft : apple).system || "linux")].name); 

And one more disclaimer. I by no means urge to draw up the source code in this way, and I even ask you never to do so. But, situations in life are different and it may be useful to be able to read different monstrous constructions. The enemy, as they say, you need to know in person. But, nevertheless, some of the ingredients of this pock can be very useful in certain situations.

Proceed, pray and in order

Objects, Properties, and Methods
 var obj = { property: 0, method: function() {} } 

This is an object with property property and method method, which can be accessed in several ways:

 obj.property; //,    obj.method(); obj["property"]; // ,    obj["method"](); 

')
Ternary operator
It is necessary for simple recording of a simple conditional construction. So, for example, the following condition
 if (a > b) { console.log("a  b"); } else { console.log("a    b"); } 

You can write shorter:
 (a > b) ? console.log("a  b") : console.log("a    b"); 

or even shorter, due to the low priority of the ternary operator:
 console.log( (a > b) ? "a  b" : "a    b" ); 


Conditional operator ||
In Javascript, it is interesting because it returns the first non-zero value, which is convenient for setting default values.
 function get_object(id) { return document.getElementById(id) || "  "; //    id   «  »,     } 
 function do_something() { return false || 0 || Math.PI || "   ?"; //    } 


We return to our sheep

In the light of the above, something in the original example becomes clearer, but we analyze it completely. So, here is our little brain tease:
 user[(os[((user.microsoft_adept ? microsoft : apple).system || "linux")].install_carma <= user.carma) ? "install" : "cant_install"](os[((user.microsoft_adept ? microsoft : apple).system || "linux")].name); 

Some ass programmer Your obedient servant decided to save several bytes of code and wrote down the task of “checking user preferences for operating systems from different manufacturers and, depending on the user's qualifications, allow him to install the system or send them to advanced training” in one line. The object model looks like this (all coincidences, of course, are random and I ask you not to be offended: all parallels are held just for lulz):
 var user = { microsoft_adept: false, //     carma: 10, //   install: function(system) { console.log("I'am installing " + system + "!"); }, cant_install: function(system) { console.log("I can't install " + system + "..."); } } var os = { windows: {  name: "Windows",  install_carma: 30 //,     }, macos: {  name: "Mac OS",  install_carma: 50 }, linux: {  name: "Linux",  install_carma: 70 } } var microsoft = { system: "windows" } var apple = { system: "macos" } 

First, we act like a famous Italian sculptor and cut off all unnecessary. The result is the following type of construction:
 user[ () ? "install" : "cant_install"](); 

It is immediately obvious that, depending on the result of some condition, the corresponding method of the user object is called. Namely, install in case the condition is true and cant_install if false. Something is passed to both methods as a parameter. I propose to deal with this neytym:
 os[((user.microsoft_adept ? microsoft : apple).system || "linux")].name; 

Something - the name of the desired operating system, depending on user preferences. Spell out: if the user prefers Microsoft products, then we read the system property of the microsoft object. If not, then the property is also read by the apple object. If such a property does not exist (vendecapetz-there or something else is not good), then the user, except for Linux, has nothing left. Thus, we get one of the three options (see object model):
 os[microsoft.system].name; // . . Windows os[apple.system].name; // . . Mac OS os["linux"].name; // . . Linux 

In the condition that determines whether the install method or cant_install is called on the user object, we dig down to the install_carma property in the same way, depending on user preferences and the “market situation” and compare the value of this property with the value of the carma property and the user object. If the karma of the user is sufficient, then everything is in order - we call the user [“install”] method. If not, then everything is fine too, but we call the user [“cant_install”] method and pass the name of the system called as a parameter.

Source code of the example in full (for copy-paste)

 var user = { microsoft_adept: false, //     carma: 10, //   install: function(system) { console.log("I'am installing " + system + "!"); }, cant_install: function(system) { console.log("I can't install " + system + "..."); } } var os = { windows: {  name: "Windows",  install_carma: 30 //,     }, macos: {  name: "Mac OS",  install_carma: 50 }, linux: {  name: "Linux",  install_carma: 70 } } var microsoft = { system: "windows" } var apple = { system: "macos" } user[(os[((user.microsoft_adept ? microsoft : apple).system || "linux")].install_carma <= user.carma) ? "install" : "cant_install"](os[((user.microsoft_adept ? microsoft : apple).system || "linux")].name); 


Conclusion

Once again, I repeat that the article is intended primarily for beginner Javascript programmers who, in their service, will have to deal with a variety of “someone else's code” that will have to be read. In addition, a number of examples of non-trivial js-syntax may be useful in some cases, but (sic!) I remind you again and again that the article gives an example of how not to do it! Try to always write beautiful and readable code. And I won’t keep myself from quoting a well-known aphorism: “Write the code, assuming that all programmers accompanying your program are violent psychopaths who know where you live.”

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


All Articles