javascript基礎(chǔ)一
JavaScript和ECMAScript的關(guān)系
ECMAScript不是一門語言,而是一個(gè)標(biāo)準(zhǔn)
符合這個(gè)標(biāo)準(zhǔn)的比較常見的有:JavaScript、Action Script(Flash中用的語言)
最新標(biāo)準(zhǔn)是ECMAScript 6版本,即ES6,語言的能力更強(qiáng),node.js完美支持
js組成
js是一款運(yùn)行在客戶端的網(wǎng)頁編程語言。
組成部分
| ecmascript | js標(biāo)準(zhǔn) js基礎(chǔ)語法 |
|---|---|
| dom | 通過js操作網(wǎng)頁元素 網(wǎng)頁中的任意標(biāo)簽被稱為dom元素 |
| bom | 通過api操作瀏覽器 |
特點(diǎn)
解釋執(zhí)行,基于對(duì)象,大小寫敏感
引入方式
內(nèi)部引入:
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="javascript" contenteditable="true" cid="n26" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><script>
var user = {
name: '張三',
age: 20,
sex: true
}
console.log(typeof user); // 顯示為object(對(duì)象)
</script></pre>
外部引入:
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="javascript" contenteditable="true" cid="n28" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><script src="main.js"></script></pre>
輸出內(nèi)容
alert()
在頁面彈出一個(gè)對(duì)話框,早期JS調(diào)試使用。
confirm()
在頁面彈出一個(gè)對(duì)話框, 常配合if判斷使用。
console.log()
將信息輸入到控制臺(tái),用于js調(diào)試。
prompt()
彈出對(duì)話框,用于接收用戶輸入的信息。
document.write()
在頁面輸出消息 幾乎不用
-
document.write不僅能輸出信息,還能輸出標(biāo)簽。
轉(zhuǎn)義字符 \
\” 轉(zhuǎn)雙引
\’ 轉(zhuǎn)單引
\n 轉(zhuǎn)換行
\r 轉(zhuǎn)回車
js注釋
快捷鍵 ctrl+/
單行注釋 //
多行注釋 /* */ ctrl+Shift +/
變量
變量是用來存儲(chǔ)數(shù)據(jù)的容器
變量類型
number
數(shù)字類型
- var age = 20;
string
字符串類型
- var name = '張三';
boolean
布爾類型
var sex1 = true ;
var sex1 = false ;
undefined
未定義類型,只聲明變量未賦值,
- var weight;
null
null就是null,只有當(dāng)值為null的時(shí)候才為null
- var exp = null;
判斷數(shù)據(jù)類型
簡單數(shù)據(jù)類型
var age = 30;
輸入console.log(typeof age); //控制臺(tái)顯示為number
復(fù)雜數(shù)據(jù)類型
var user = { ? name: '張三', ? age: 20, ? sex: true ? }
console.log(typeof user); // 顯示為object(對(duì)象)
instanceof
判斷某個(gè)對(duì)象是否為某個(gè)構(gòu)造函數(shù)的實(shí)例對(duì)象
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="javascript" contenteditable="true" cid="n119" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">//復(fù)雜數(shù)據(jù)類型 數(shù)組 Array
var ages = [33,20,555];
console.log(typeof ages); // object
console.log(user instanceof Array)// false
console.log(ages instanceof Array)// true</pre>
規(guī)范
不能以數(shù)字或者純數(shù)字開頭來定義變量名。
不推薦使用中文來定義變量名。
不能使用特殊符號(hào)或者特殊符號(hào)開頭(_除外);
不推薦使用關(guān)鍵字和保留字來定義變量名。
在JS中嚴(yán)格區(qū)分大小寫的!
避免使用的關(guān)鍵詞
| break | do | instanceof | typeof |
|---|---|---|---|
| case | else | new | var |
| catch | finally | return | void |
| continue | for | switch | while |
| debugger | function | this | with |
| default | if | throw | delete |
| in | try |
比較運(yùn)算符
小于 <
大于 >
小于等于 <=
大于等于 >=
比較運(yùn)算 ==
不等于 !=
-
注意:
= 是賦值運(yùn)算符
==才是比較運(yùn)算符
算數(shù)運(yùn)算符
-
- 號(hào)
數(shù)字類型相加==> 數(shù)字求和
number類型+ string類型 ==> 拼接 string類型
string類型 直接拼接
-
- 號(hào)
number-字符串number可以把stirng轉(zhuǎn)換為number運(yùn)算
console.log(3/0); // Infinity 無限大
console.log(3-'張三'); // NaN ; not a number 提示縮寫
-
console.log(NaN == NaN); // false 面試:
- NaN永遠(yuǎn)不等于NaN
/ 除號(hào)
-
% 求余
- 重要
-
操作符
var a = 10; var b = a+20; console.log(b);輸出30.
c = c + 1等同于 c += 1等同于 c++
github
全球最大的程序員交友網(wǎng).
使用教程
注冊github
創(chuàng)建庫,不要自動(dòng)生成readme文件,手動(dòng)生成.
-
安裝git:
一個(gè)本地的軟件版本控制器.當(dāng)前本地連接github必須使用git.
git操作:
進(jìn)入創(chuàng)建的文件夾 xxx-learn-note, 右鍵==> git bash here ==> 黑窗口
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="shell" contenteditable="true" cid="n243" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">輸入以下代碼:
git -init
在當(dāng)前文件夾創(chuàng)建.git的隱藏文件夾.這里實(shí)際是初始化本地倉庫.
git add .
添加當(dāng)前文件夾現(xiàn)有文件,不包括空文件夾
git commit -m "第一次提交"
如果是第一次使用將會(huì)讓添加郵箱和密碼,按提示操作
git remote add origin git@github.com:Viarotel/fengxiaotian-learn-note.git
添加本地源,鏈接github服務(wù)器
git push -u origin master
將本地倉庫信息推送到遠(yuǎn)程倉庫
</pre>
常用git操作命令:
git status 查看工作區(qū)狀態(tài),紅色說明有變動(dòng),綠色說明全部被提交到暫存區(qū)
git config –global user.email “you@example.com”(配置賬號(hào))
git config –global user.name “Your Name”(配置賬號(hào))
mkdir xxx (創(chuàng)建文件夾xxx)
cd xxx (切換到xxx目錄)
git init(初始化 git 倉庫)
git status(查看狀態(tài))
git add . (這里“.”代表全部添加到上傳列表)
git commit -m ‘xxx.md’(提交,“”里面的內(nèi)容是提交的信息)
git log(查看所有產(chǎn)生的 commit 記錄)
git branch(查看本地分支)
git branch -r(查看遠(yuǎn)程分支列表)
git branch xx(創(chuàng)建分支xx)
git checkout xx(進(jìn)入分支xx)
git checkout -b xx(新建一個(gè)分支,自動(dòng)切換到該分支)
git merge xx(合并分支至當(dāng)前分支)
git rebase xx(合并分支至當(dāng)前分支)
git branch -d xx(刪除分支)
git branch -D xx(強(qiáng)制刪除分支)
git tag (查看標(biāo)簽)
git tag xx(新建標(biāo)簽)
git checkout xx(進(jìn)入標(biāo)簽)
ssh-keygen -t rsa(指定 rsa 算法生成密鑰,這里是在git-bash里面運(yùn)行,用于生成鏈接git與電腦的密匙)
git push origin master(把本地代碼推到遠(yuǎn)程 master 分支)
git pull origin master(把遠(yuǎn)程最新的代碼更新到本地)
git clone git@github.com:name/xx.git(把xx項(xiàng)目 clone 到本地)
git remote add .origin. git@github.com:name/x.git(本地項(xiàng)目與遠(yuǎn)成倉庫關(guān)聯(lián))
git remote -v(查看當(dāng)前項(xiàng)目的遠(yuǎn)程庫)
git config –global alias.xx .checkout.(設(shè)置命令別名)
git diff <id1><id1><id2> (比較兩次提交之間的差異)
git diff .. (在兩個(gè)分支之間比較)
git diff –staged (比較暫存區(qū)和版本庫差異)
git stash(植入暫存區(qū))
git stash list(查看暫存區(qū)記錄)
git stash apply(植出暫存區(qū))
git stash drop(刪除暫存區(qū)最近一條記錄)
git stash pop(apply加drop功能集合)
git stash clear(清空暫存區(qū))