码迷,mamicode.com
首页 > Web开发 > 详细

you don't know js -- up &going 笔记

时间:2017-02-15 14:39:51      阅读:306      评论:0      收藏:0      [点我收藏+]

标签:ima   erp   bool   not   ica   bsp   tac   cat   string   

Code

  A program, often referred to source code or just code , is a set of special instructions to tell the computer what tasks to perform. Usually, the code is saved in a text file, although with JavaScript you can type code directly into a developer console in a browser.  The rules for valid format and combinations of instruction is called computer language, sometimes referred as its syntax, much the same as English tells you how to spell words and how to create valid sentences using words and punctuation.

Statements

  In a computed language, a group of words, numbers and operators that performs a specific task is a statement, such as a = b *2. 

  The characters a and b are called Variables, which are like simple boxes to store any of your stuff in. In programs ,variable holds the values to be used by program. Think of them as symbolic placeholders for values themselves.

  By contrast, the 2 is just a value itself, called a literal value,  because it stands alone without being stored in a variable. 

  The = and * characters are operators, they perform actions with the values and variables such as assignment and mathematic multiplication.

  The statement a = b *2; tells the computer to get the current value stored in the variable b, multiply that value by 2, then store the result back into another variable a.

  Programs are just collections of many such statements , which together describe all the steps that it takes to perform your program’s purpose.

Expression

  Statements are made up of one or more expressions. An expression is any reference to variable or value, or a set of variables and values combined with operators. For example  a= b*2; This statement has four expressions in it:

    2 is a literal value expression , b is a variable expression, which means to retrieve its current value

    b*2 is an arithmetic expression, which means to do the multiplication.

    a = b *2 is an assignment expression, which means to assign the result of the b *2 to the variable a.

     A general expression that stands alone is also called an expression statement, such as the following : b *2; This flavor of expression statement is not very common or useful, as generally it would not have any effect on the running  of the program – it would retrieve the current value of b and multiply it by 2, but then wouldn’t do anything with that result.

     A more common expression statement is a call expression statement, as the entire statement is the function call expression itself: alert(“a”);

Executing a Program

  How do those collections of programming statements tell the computer what to do? The program needs to be executed, also referred to as running the program.

    Statement like a = b*2  are helpful for developers when reading and writing, but are not acturally in a form the computer can directly understand . So a special utility on the computer (either an interpreter or a complier) is used to translate the code you write into commands a computer can understand. 

  For some computer languages, this translation of commands is typically done from top to bottom, line by line, every time the program is run, which is usually called interpreting the code.

  For other languages, the translation is done ahead of time, called compiling the code, so when the program runs later, what’s running is actually the already complied computer instructions ready to go. 

  It’s typically asserted that JavaScript is interpreted, because your JavaScript source code is processed each time it’s run. But that’s not entirely accurate. The JavaScript engine actually compiles the program on the fly and then immediately runs the compiled code.

    Note : To type multiple lines into the console at once, use  <shift> + <enter> to move to the nextnew line. Once you hit  <enter> by itself, the console will run everything you’ve just typed.

Value and type  

  When you express values in a program, you choose different representations for those values based on what you plan to do with them. These different representations for values are called types in programming terminology. 

    When you need to do math, you want a number.

        When you need to print a value on the screen, you need a string.

        When you need to make a decision in your program, you need a Boolean.

Converting Between Types 

  If you have a number but need to print it on the screen, you need to convert the value to a string. Similarly, if someone enters a series of numeric characters into a form, that’s string, but if you need to then use that value to do math operations, you need to coerce it to a number.  explicit coerceion  and implicit coercion

Code comment 

Variables 

  Most useful programs need to track a value as it changes over the course of the program, undergoing different operations as called for by your program’s intended tasks.

    The easiest way to  go about that in your program is to assign a value to a symbolic container  called a variables ----so called because the value in this container can vary over time as needed. The variable holds a running value that changes over the course of the program, illustrating the primary purpose of variables :managing program state.

    In other words, state is tracking the changes to values as your program runs.

  Another common usage of variables is for centralizing value setting. This is more typically called constants, when you declare a variable with a value and intend for that value to not change throughout the program.

  You declare these constants, often at the top of a program, so that it’s convenient for you to have one place to go to alter a value if you need to. By convention, JavaScript variables as constants are usually capitalized, with underscores _ between multiple words, such as var TAX_RATE = 0.08;

Conditions 条件语句/loop 循环语句 => block (语句块) => Scope 

  We often need to group a series of statements together, which we often call a block.

{
    var amount = 99;
    amount = amount *2;
    console.log(amount);
}

  This kind of standalone {..} general block is valid but is not commonly seen in Js programs. Typically, blocks are attached to some other control statement, such as an if statement or a loop.

Closure 

  You can think of closure as a way to “remember” and continue to access a function’s scope(its variables) even once the function has finished running.

function makeAdder(x) {
    
    function add(y) {
        return x + y;
         }

       return  add;
}

 var plusOne = makeAdder(1);  
// plusOne gets a reference to the inner “add(..)” function with closure over the ‘x’ parameter of the outer “makeAdder(..)”
 plusOne(3)   // 4

  The reference to add(..) function that gets returned with each call to the outer makeAdder(.. ) is able to remember whatever x value was passed in to makeAdder(…)

  When we call MakeAdder(1), we get back a reference to its inner add(..)  that remember x as 1. We call this function reference plusOne(…);

    When we call plusOne(3), it adds 3(its inner y ) to the 1(remembered by x ) and we get 4 as the result.

 

you don't know js -- up &going 笔记

标签:ima   erp   bool   not   ica   bsp   tac   cat   string   

原文地址:http://www.cnblogs.com/SamWeb/p/6401106.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!