「譯」JavaScript 概述 Introduction to JavaScript

譯自:codecademy

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 的部分又固。

This example logs?5?to the console.?

分號(hào)表示the end of the line, or statement. 盡管不寫分號(hào)也能生效,但是為了養(yǎng)成好習(xí)慣煤率,還是寫上吧仰冠。

Comments 注釋

注釋是給人看的(不是機(jī)器)

單行注釋以雙斜線開頭
也可以在一行代碼后使用
多行注釋以/*開頭,*/結(jié)尾


你也可以用這個(gè)句法注解掉一行代碼中間的某一部分

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ù)):?%

前四種符號(hào)含義不言自明

注意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ù)狞贱。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末刻获,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子瞎嬉,更是在濱河造成了極大的恐慌蝎毡,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,548評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件佑颇,死亡現(xiàn)場(chǎng)離奇詭異顶掉,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)挑胸,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,497評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門痒筒,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事簿透∫婆郏” “怎么了?”我有些...
    開封第一講書人閱讀 167,990評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵老充,是天一觀的道長(zhǎng)葡盗。 經(jīng)常有香客問我,道長(zhǎng)啡浊,這世上最難降的妖魔是什么觅够? 我笑而不...
    開封第一講書人閱讀 59,618評(píng)論 1 296
  • 正文 為了忘掉前任,我火速辦了婚禮巷嚣,結(jié)果婚禮上喘先,老公的妹妹穿的比我還像新娘。我一直安慰自己廷粒,他們只是感情好窘拯,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,618評(píng)論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著坝茎,像睡著了一般涤姊。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上嗤放,一...
    開封第一講書人閱讀 52,246評(píng)論 1 308
  • 那天思喊,我揣著相機(jī)與錄音,去河邊找鬼次酌。 笑死搔涝,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的和措。 我是一名探鬼主播,決...
    沈念sama閱讀 40,819評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼蜕煌,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼派阱!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起斜纪,我...
    開封第一講書人閱讀 39,725評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤贫母,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后盒刚,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體腺劣,經(jīng)...
    沈念sama閱讀 46,268評(píng)論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,356評(píng)論 3 340
  • 正文 我和宋清朗相戀三年因块,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了橘原。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片势木。...
    茶點(diǎn)故事閱讀 40,488評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖甲锡,靈堂內(nèi)的尸體忽然破棺而出噩咪,到底是詐尸還是另有隱情,我是刑警寧澤芋酌,帶...
    沈念sama閱讀 36,181評(píng)論 5 350
  • 正文 年R本政府宣布增显,位于F島的核電站,受9級(jí)特大地震影響脐帝,放射性物質(zhì)發(fā)生泄漏同云。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,862評(píng)論 3 333
  • 文/蒙蒙 一堵腹、第九天 我趴在偏房一處隱蔽的房頂上張望炸站。 院中可真熱鬧,春花似錦秸滴、人聲如沸武契。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,331評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)咒唆。三九已至,卻和暖如春释液,著一層夾襖步出監(jiān)牢的瞬間全释,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,445評(píng)論 1 272
  • 我被黑心中介騙來泰國(guó)打工误债, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留浸船,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,897評(píng)論 3 376
  • 正文 我出身青樓寝蹈,卻偏偏與公主長(zhǎng)得像李命,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子箫老,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,500評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容