Be a geek-learning programming from scratch 3: ubiquitous javascript 2

Source: Internet
Author: User

Ubiquitous Javascript

Javascript is everywhere now. Maybe a website you are opening may be node. js + json + javascript + mustache. although you have not understood what the above is, it is precisely because you do not understand it that you need to learn more. However, Javascript is everywhere. It may run in a process in your IDE in an app on your mobile phone, on the Web page you browse.

Starting from mathematics

Let's go back to chapter 1's question about James,Programming from actual problems makes it easier to learn Programming. In the elementary school age, the math problems most like this: the sugar in a store costs 5 yuan, James bought 3 sugar, and James spent a total of how much money. Maybe we are still elementary school students in programming. The most direct method is to directly calculate 3x5 =?

document.write(3*5);

Document. write can also be understood as output, that is, the result of writing 3*5 to the page will be output in double quotation marks. We will see 15 in the browser, which is a good start and a bad start. (Reprinted and retained: be a geek 3: ubiquitous javascript 2)

Design and Programming

If our actual problems always end with the expected results, we will become a code monkey many years later. Design the problem once. The so-called design sometimes complicate the simple problem, and sometimes make the expansion easier in the future. On this day, because the store's sugar price is too high, the store manager reduced the price to 4 yuan.

document.write(3*4);

So we got our results again, but the next time we saw the code, we didn't know which is the quantity of sugar and which is the price, so we re-designed the program.

tang=4;num=3;document.write(tang*num);

This can be called programming. Maybe you have noticed the existence of the symbol ";". What I want to say is that this is another standard and we have to comply with it and have to perform fuck.

Function

Remember that when we first learned the trigonometric function, we wrote

sin 30=0.5

Our functions are similar to this. In other words, because many pioneers in computer science have learned mathematics well and brought the laws of the mathematical world to the computer world, our functions are similar to this, let's make a simple start.

function hello(){    return document.write("hello,world");}hello();

When I first saw the function, I was a little excited. We wrote a function named hello, which returns the method for writing hello and world to the page. Then we call the hello function, so there is hello and world on the page.

function sin(degree){    return document.write(Math.sin(degree));}sin(30);

Here degree is called a variable, that is, the amount that can be changed. The output is-0.9880316240928602 instead of 0.5, because here we use radians instead of angle.

sin(30)

The output result of is somewhat similar to sin 30. The purpose of writing parentheses is to facilitate parsing. This may be different in different languages. For example, in ruby, we can directly use expressions similar to those in mathematics:

2.0.0-p353 :004 > Math.sin 30=> -0.98803162409286182.0.0-p353 :005 >

We can input multiple variables in the function, so we will go back to Xiaoming's question and write the code like this.

function calc(tang,num){    result=tang*num;    document.write(result);}calc(3,4);

However, to some extent, our calc has done computation and output. In general, the design is poor.

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.