(1)JavaScript語句組成的:
Values, Operators, Expressions, Keywords, and Comments.
值,運(yùn)算符,表達(dá)式,關(guān)鍵字和注釋。
值:
JavaScript的值>JavaScript的語法定義了兩種類型的值:固定值和變量值。
固定值稱為文字。變量值被稱為變量。
備注:編程中,變量命名有三種:Hyphns、underscore、CamelCase
在編程語言中,特別是在JavaScript中,命名通常始于一個(gè)小寫字母:
eg: firstName, lastName, masterCard, interCity.
變量命名:連字符(Hyphens)是不允許在JavaScript。這是減法。
(2)JavaScript使用Unicode字符集。
(3)關(guān)鍵字:
Keyword Description
break Terminates a switch or a loop
continue Jumps out of a loop and starts at the top
debugger Stops the execution of JavaScript, and calls (if available) the debugging function
do ... while Executes a block of statements, and repeats the block, while a condition is true
for Marks a block of statements to be executed, as long as a condition is true
function Declares a function
if ... else Marks a block of statements to be executed, depending on a condition
return Exits a function
switch Marks a block of statements to be executed, depending on different cases
try ... catch Implements error handling to a block of statements
var Declares a variable
(4)注釋:
單行注釋://
多行注釋:/*...*/
(5) JavaScript的數(shù)據(jù)類型
var length = 16; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // 數(shù)字
var lastName = "Johnson"; ? ? ? ? ? ? ? ? ? ? ?// 字符串
var cars = ["Saab", "Volvo", "BMW"]; ? ? ? ? ? // 數(shù)組
var x = {firstName:"John", lastName:"Doe"}; ? ?// 對(duì)象
JavaScript 有動(dòng)態(tài)數(shù)據(jù)類型
var X ;
var X=5 ;
var X ="Jin";
一個(gè)字符串(或一個(gè)文本字符串)是一系列的字符,如“美國能源部”。字符串是用引號(hào)寫的。可以使用單引號(hào)或雙引號(hào):
var carName = "Volvo XC60"; ? // 雙引號(hào)
var carName = 'Volvo XC60'; ? // 單引號(hào)
在字符串中使用引號(hào),只要不匹配字符串周圍的引號(hào):
var answer = "It's alright"; ? ? ? ? ? ? // 單引號(hào)在雙引號(hào)的里面
var answer = "He is called 'Johnny'"; ? ?// 單引號(hào)在雙引號(hào)的里面
var answer = 'He is called "Johnny"'; ? ?// 雙引號(hào)在單引號(hào)的里面
JavaScript Booleans
true false
JavaScript 對(duì)象
var person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};
JavaScript typeof
typeof "John" ? ? ? ? ? ? ? ?// 返回字符串
typeof 3.14 ? ? ? ? ? ? ? ? ?// 返回?cái)?shù)字
typeof false ? ? ? ? ? ? ? ? // 返回 boolean
typeof [1,2,3,4] ? ? ? ? ? ? // 返回 object
typeof {name:'John', age:34} // 返回 object
區(qū)別 Undefined and Null
typeof undefined ? ? ? ? ? ? // undefined
typeof null ? ? ? ? ? ? ? ? ?// object
null === undefined ? ? ? ? ? // false
null == undefined ? ? ? ? ? ?// true
(6)Function
允許 a function 無()的訪問函數(shù)將返回函數(shù)定義:
eg:
function toCelsius(fahrenheit) {
return (5/9) * (fahrenheit-32);
}
document.getElementById("demo").innerHTML = toCelsius;
Functions 作為變量使用:
eg:
var text = "The temperature is " + toCelsius(77) + " Celsius";
var x = toCelsius(32);
var text = "The temperature is " + x + " Celsius";
(7)JavaScript Objection
object 創(chuàng)建
eg:
var car = "Fiat";
var car = {type:"Fiat", model:"500", color:"white"};
object 屬性的兩種訪問方式:
objectName.propertyName
objectName[propertyName]
object 方法的訪問:
objectName.methodName()
不要申明Strings, Numbers, Boolean為對(duì)象!
var x =newString();// Declares x as a String object
var y =newNumber();// Declares y as a Number object
var z =newBoolean();// Declares z as a Boolean object
避免字符串、數(shù)字和布爾對(duì)象。它們使你的代碼復(fù)雜化,并減慢執(zhí)行速度。
(8)JavaScript訪問字符集
全局變量:
var carName =" Volvo";
// code here can use carName
functionmyFunction() {
// code here can usecarName
}
自動(dòng)全局化變量:
// code here can use carName
function myFunction() {
carName ="Volvo";
// code here can use carName
}
一般不要?jiǎng)?chuàng)建全局變量。