Encounter cascading expression traps _javascript techniques in JavaScript

Source: Internet
Author: User
Vaguely remember, in learning C language library functions, many string operations related to the function will return the result-related pointers. In fact, this return value is very often not very necessary, because in our incoming parameters, ten of them already contain this pointer. And the best thing about this return value is that we can write a cascading expression for our handy book. But in the actual work of these years, it is increasingly felt that the cascade expression is a devil pie.

For example, in the C language, we are familiar with string manipulation functions such as STRCPY,STRCAT, their prototype is generally: extern char *strxxx (char *dest, char *src);
The return value char* is actually the *dest in the call parameter, so that you can easily write the cascading expression, as follows:
Char *title = "Mr.";
Char *name = "Birdshome";
int len = strlen (strcat (title, name));
In object-oriented programming, we can write chained expressions by returning an object by means of a method. Even if it is cascading expressions or chain expressions, it can make us more convenient to write code, but if used improperly, it will be very depressing. Especially for cascading expressions, if the function is nested too much, not easy to understand, debug is also very depressing.

The following JavaScript cascading statement, let me depressed for a long time ... Diminfo.push (Stringhelper.arraytostring (Item.m_dimensionname,
Item.m_dimensionuniquename, Item.m_analysisstatus,
(item.m_isparameterized?) ' Checked ': '), Item.m_dimensiontype, leveltypes);
And the correct statement should be the following: Diminfo.push (stringhelper.arraytostring item.m_dimensionname,
Item.m_dimensionuniquename, Item.m_analysisstatus,
(item.m_isparameterized?) ' Checked ': '), Item.m_dimensiontype, leveltypes);
The problem is in the penultimate bracket ")", originally this bracket should be in the parameter leveltypes, the result did not pay attention to get leveltypes before, this kind of writing error, it is difficult to see out. More depressing is that javascript on the number of functions, as well as have no parameters are not interested, so the wrong statement can run "normal", but after the data to the background, how can not get the required value, always undefined.

In addition, there is the compound parameter call statement, if it can be properly unfolded will bring us a lot of benefits, such as code:
var rect = Dashboard.getboundingclientrect ();
This. Insertnewroom (Dashboard, event.clientx-rect.left-1, Event.clienty-rect.top, event);
The code that expands the composite parameter is: var rect = Dashboard.getboundingclientrect ();
var Innerx = event.clientx-rect.left-1;
var innery = event.clienty-rect.top;
This. Insertnewroom (Dashboard, Innerx, Innery, event);
     Although this expanded code does not add any additional logic, statements that add temporary variables Innerx and innery are obviously much easier to understand than statements of composite arguments. This code is much more, but it makes the code have self-documented characteristics, but also does not change the logic and efficiency of the code. I believe that when you debug or modify someone else's code, you want to see the latter one.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.