JS Foundation--A small example of function reference (commodity price calculation) __ function

Source: Internet
Author: User

In the process of writing the front-end code, often found that there are many functions of the code is repeated, for example, in the home page of NetEase, there are several tabs, the function of these tabs is the same, that is, when the mouse clicks or strokes the past, the following content display will display corresponding content, The difference is the number of tabs and the text content of the tabs. So in the process of writing this part of the code, we can reuse the code as much as possible, without causing too much redundant code.

Reusing code:
1, as far as possible to ensure that the HTML code structure consistent, you can select child elements through the parent;
2, the core of the main program to achieve, wrapped up with functions;
3, to find out the different values in each group, through the transfer of parameters to achieve;

Here is a simple example, for example, when we are in the statistical price of goods, we only know the price of goods and the quantity of goods can directly calculate the total price of this product, then for different products, the calculation method is the same, this time we can reuse code, Without having to write too much code to implement this functionality. As shown in the figure:


Implementation code:

<!
    DOCTYPE html> Description

1, the number of goods should be directly read the number of pages set, because different site preset initial value may not be the same, such as 0 or 1;

2, in the reduction of goods, pay attention to determine whether the quantity of goods is <0, because the number of <0, then the commodity price is negative, that is to pay the money ... ;


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.