Console 控制臺(tái)
Console是一個(gè)為開發(fā)者展示重要信息(like errors)的面板邮府。大多數(shù)計(jì)算機(jī)對(duì)代碼進(jìn)行的操作是不可見的宵距,若想看見,可以直接在console上print, or?log负芋。
在JS中督惰, the?console?keyword 指?an object,a collection of data and actions, that we can use in our code.?Keywords are words that are built into the JavaScript language, so the computer will recognize them and treats them specially.
One action, or method, 一旦被建立在 the?console?object 中若锁,它就是 the?.log()?method. 當(dāng)我們寫下console.log() 時(shí),放入圓括號(hào)內(nèi)的斧吐,即是將要被printed, or logged, to the console 的部分又固。
分號(hào)表示the end of the line, or statement. 盡管不寫分號(hào)也能生效,但是為了養(yǎng)成好習(xí)慣煤率,還是寫上吧仰冠。
Comments 注釋
注釋是給人看的(不是機(jī)器)
Data Types 數(shù)據(jù)類型
JS中蝶糯,有 7 種?fundamental data types :
1.Number: Any number, including numbers with decimals(小數(shù)):?4,?8,?1516,?23.42.
2.String(字符串): Any grouping of characters on your keyboard (letters, numbers, spaces, symbols, etc.) surrounded by single quotes:?' ... '?or double quotes?" ... ". Though we prefer single quotes. Some people like to think of string as a fancy word for text.
3.Boolean(布爾值): This data type only has two possible values— either?true or?false?(without quotes). It’s helpful to think of booleans as on and off switches or as the answers to a “yes” or “no” question.
4.Null: This data type represents the intentional(故意的)absence of a value, and is represented by the keyword?null?(without quotes).
5.Undefined: This data type is denoted by the keyword?undefined(without quotes). It also represents the absence of a value though it has a different use than?null.
6.Symbol: A newer feature to the language, symbols are unique identifiers(標(biāo)識(shí)符), useful in more complex coding. No need to worry about these for now.
7.Object: Collections of related data.
前6種類型被稱為primitive data types洋只,它們是JS中?most basic data types。Objects?較為復(fù)雜裳涛,當(dāng)你深入學(xué)習(xí)JS時(shí)自然會(huì)了解木张。7種類型看起來似乎不多,但隨后你將會(huì)發(fā)現(xiàn)端三,當(dāng)你利用它們每一種時(shí),將會(huì)創(chuàng)造不一樣的世界鹃彻。隨著你更多地了解objects郊闯,你將會(huì)創(chuàng)造?complex collections of data.
上例中,我們首先寫入了一個(gè)字符串(string)蛛株,它不只是是一個(gè)單詞团赁,它包括capital and lowercase letters(大小寫字母), spaces, and punctuation(標(biāo)點(diǎn)符號(hào)).
Arithmetic Operators 算數(shù)運(yùn)算符
An?operator?is a character(符號(hào))?that performs a task in our code.
JS有幾個(gè)內(nèi)置的?arithmetic operators,它們可以助你進(jìn)行數(shù)學(xué)運(yùn)算:
Add:?+
Subtract:?-
Multiply:?*
Divide:?/
Remainder(求余數(shù)):?%
注意console.log()將會(huì)計(jì)算評(píng)估(evaluate)括號(hào)內(nèi)的表達(dá)式谨履,然后輸出結(jié)果至console欢摄。
String Concatenation 字符串聯(lián)接
Operators 不僅可以用于數(shù)字,當(dāng)+?operator 用于兩個(gè)strings中時(shí)笋粟,它將會(huì)將在左邊的string后面追加右面的string怀挠,這個(gè)過程稱為concatenation(串聯(lián),聯(lián)接):
在第三個(gè)例子中害捕,若需兩個(gè)strings之間有空格绿淋,則要在第一個(gè)string后面加入空格。
Properties 屬性
當(dāng)你在JS中引入一段data時(shí)尝盼,瀏覽器將其作為?an instance of the data type 來保存吞滞。每個(gè)?string instance 都有一個(gè) length 屬性,它存儲(chǔ)著該string的字母?jìng)€(gè)數(shù)盾沫。你可以通過在這個(gè)string后面(append)追加a period and the property name裁赠,來檢索(retrieve)這個(gè)屬性信息?:
The?.?is another operator! We call it the?dot operator.
上例中殿漠,這個(gè)progra將輸出(print)至console,因?yàn)镠ello有5個(gè)字母佩捞。
Methods 方法
Remember that methods are actions we can perform. JavaScript provides a number of string methods.
We?call, or use, these methods by appending(在后面追加) an instance with a period (the dot operator), the name of the method, and opening and closing parentheses:
ie.?'example string'.methodName().
這個(gè)語法是否似曾相識(shí)绞幌?當(dāng)我們使用?console.log() 時(shí),we’re calling the?.log()?method on the?console?object.?
?Let’s see?console.log()?and some real string methods in action!
On the first line, the?.toUpperCase()?method is called on the string instance?'hello'. The result is logged to the console. This method returns a string in all capital letters:?'HELLO'.
On the second line, the?.startsWith()?method is called on the string instance?'Hey'. This method also accepts the character?'H'as an input, or?argument, between the parentheses. Since the string?'Hey'?does start with the letter?'H', the method returns the boolean?true.
Built-in Objects 內(nèi)置對(duì)象
除了console失尖,JS中還有其他內(nèi)置的objects啊奄。你可以建立your own objects,但是對(duì)這些新的“built-in” objects都有其特定的設(shè)計(jì)目的?(functionality)掀潮。
例如菇夸,你想執(zhí)行一復(fù)雜的數(shù)學(xué)運(yùn)算(不是簡(jiǎn)單的加減乘除),JS有?the built-in?Math?object.
Objects 的優(yōu)勢(shì)是他們有 methods仪吧!Let’s call the?.random()?method from the built-in?Math?object:
上例中庄新,我們?cè)?the object name (Math)之后添加了?.random()?method 。該 method 將在0和1之間隨機(jī)返回一個(gè)數(shù)字薯鼠。
要在0~50之間生成一個(gè)隨機(jī)數(shù)择诈,我們可以將結(jié)果乘以(multiply)50,如下:
上例中我們可能會(huì)得到小數(shù)出皇。為確保答案是一個(gè)整數(shù)(a whole number)羞芍,我們可以利用另一個(gè)useful?Math?method ——?Math.floor().
一旦Math.floor()得到小數(shù),它將四舍五入冉妓摇(round)最近的整數(shù)(whole number)荷科,如下:
上例中:
Math.random 生成了0和1之間的隨機(jī)數(shù)。
當(dāng)我們乘以50后纱注,將得到0~50之間的數(shù)字畏浆。
然后,Math.floor()將四舍五入取整數(shù)狞贱。