Console 控制台
Console是一个为开发者展示重要信息(like errors)的面板。大多数计算机对代码进行的操作是不可见的,若想看见,可以直接在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. 当我们写下console.log() 时,放入圆括号内的,即是将要被printed, or logged, to the console 的部分。
分号表示the end of the line, or statement. 尽管不写分号也能生效,但是为了养成好习惯,还是写上吧。
Comments 注释
注释是给人看的(不是机器)
Data Types 数据类型
JS中,有 7 种 fundamental data types :
1.Number: Any number, including numbers with decimals(小数): 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(标识符), 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 较为复杂,当你深入学习JS时自然会了解。7种类型看起来似乎不多,但随后你将会发现,当你利用它们每一种时,将会创造不一样的世界。随着你更多地了解objects,你将会创造 complex collections of data.
上例中,我们首先写入了一个字符串(string),它不只是是一个单词,它包括capital and lowercase letters(大小写字母), spaces, and punctuation(标点符号).
Arithmetic Operators 算数运算符
An operator is a character(符号) that performs a task in our code.
JS有几个内置的 arithmetic operators,它们可以助你进行数学运算:
Add: +
Subtract: -
Multiply: *
Divide: /
Remainder(求余数): %
注意console.log()将会计算评估(evaluate)括号内的表达式,然后输出结果至console。
String Concatenation 字符串联接
Operators 不仅可以用于数字,当+ operator 用于两个strings中时,它将会将在左边的string后面追加右面的string,这个过程称为concatenation(串联,联接):
在第三个例子中,若需两个strings之间有空格,则要在第一个string后面加入空格。
Properties 属性
当你在JS中引入一段data时,浏览器将其作为 an instance of the data type 来保存。每个 string instance 都有一个 length 属性,它存储着该string的字母个数。你可以通过在这个string后面(append)追加a period and the property name,来检索(retrieve)这个属性信息 :
The . is another operator! We call it the dot operator.
上例中,这个progra将输出(print)至console,因为Hello有5个字母。
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().
这个语法是否似曾相识?当我们使用 console.log() 时,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 内置对象
除了console,JS中还有其他内置的objects。你可以建立your own objects,但是对这些新的“built-in” objects都有其特定的设计目的 (functionality)。
例如,你想执行一复杂的数学运算(不是简单的加减乘除),JS有 the built-in Math object.
Objects 的优势是他们有 methods!Let’s call the .random() method from the built-in Math object:
上例中,我们在 the object name (Math)之后添加了 .random() method 。该 method 将在0和1之间随机返回一个数字。
要在0~50之间生成一个随机数,我们可以将结果乘以(multiply)50,如下:
上例中我们可能会得到小数。为确保答案是一个整数(a whole number),我们可以利用另一个useful Math method —— Math.floor().
一旦Math.floor()得到小数,它将四舍五入取(round)最近的整数(whole number),如下:
上例中:
Math.random 生成了0和1之间的随机数。
当我们乘以50后,将得到0~50之间的数字。
然后,Math.floor()将四舍五入取整数。