Math.random():隨機取得亂數

Math.random():隨機取得亂數

const oneMathRandom=Math.random();
console.log(oneMathRandom);
//0.5852392367031296

Math.floor():取得整數

const twoMathFloorRandom= Math.floor(Math.random() * 5);
console.log(twoMathFloorRandom);
//4

Math.ceil()無條件進位

Math.ceil(Math.random()*10);
// 獲取從 1 到 10 的隨機整數,取 0 的概率極小。

1
2
3
4
5
Math.ceil(Math.random()*10);  
console.log(Math.ceil(Math.random()*10))
//2
//1
//9

Math.round():四捨五入

Math.round(Math.random());

1
2
// 可均衡獲取 0 到 1 的隨機整數。
console.log(Math.round(Math.random()); )
1
2
//Math.floor(Math.random()*10); 
//可均衡獲取 0 到 9 的隨機整數。

javascript(ES6)擴展運算子 Spread Operator

讀出第一層數據

1
2
3
4
const number=["1","2","3"];
let names = [...number];
console.log(names)
//(3) ['1', '2', '3']
1
2
3
4
const data={name:'lara',age:3};
let newdata = { ...data };
console.log(newdata)
//{name: 'lara', age: 3}

合併:等同於concat

1
2
3
4
5
var number1=[1,2,3];
var number2=[4,5,6];
let newnumber = [...number1,...number2];
console.log(newnumber)
//(6) [1, 2, 3, 4, 5, 6]

合併复制数组或对象内部数据:等同於concat

1
2
3
4
5
6
7
8
9
10
var data1={name:'Test'};
var data2={age:5};
let newData3 = {...data1,...data2};
console.log(newData3)
//{name: 'Test', age: 5}

const parts = ['shoulders', 'knees'];
const lyrics = ['head', ...parts, 'and', 'toes'];
console.log(lyrics)
(5) ['head', 'shoulders', 'knees', 'and', 'toes']

字符串轉字符数组 =>類似于 split() 方法

1
2
3
4
5
6
7
8
var one='Hello Word';
var array1 =[...one]
console.log('字符串轉字符数组',array1)
//(10) ['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'd']

let text = [...'Hello'];
console.log(text);
//(5) ['H', 'e', 'l', 'l', 'o']

當你有一個接受數字的函數,並且你將這些數字作為數組的元素時,以前的实现是使用 apply()相同

1
2
3
4
5
6
let scores = [12, 33, 6]
const addAll = (a, b, c) => {
console.log(a + b + c);
};
addAll(...scores);
//51

使用在陣列的 push 方法上

1
2
3
4
let list = [1, 2];
list.push(...[3, 4]);
console.log(list)
//(4) [1, 2, 3, 4]

Base64 encode & decode(加密與解密)

安裝: Javascript base64

1
2
3
4
5
6
7
//若要對UTF-8字串base64編碼改用js-base64套件來進行base64編碼及解碼
//加密
let base64EncodedString = window.btoa("hello world");
console.log(base64EncodedString); // aGVsbG8gd29ybGQ=
//解密
let base64DecodedString = window.atob(base64EncodedString);
console.log(base64DecodedString); // hello world

安裝: npm install –save js-base64

1
npm install --save js-base64

頁面使用

引入
import { Base64 } from ‘js-base64’;

加密:Base64.encode(‘值’)

1
2
3
const Chinese = Base64.encode('中文')
console.log(Chinese)
// "5Lit5paH"

解密:Base64.decode(‘值’)

1
2
3
const ChineseDecode = Base64.decode('5Lit5paH')
console.log(ChineseDecode)
// '中文'

React map()

//map() Key 綁定

1
2
3
4
5
6
7
8
9
10
11
12
13
14
const list=[1,2,3,4,5,6,7]

const arrayList=list.map((list,index)=>{
return (
<li key={index}>
{list}
</li>
)
})


const root = ReactDOM.createRoot(document.getElementById('root'));

root.render( <ul>{arrayList}</ul>);

React props

React props

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
html

<div id="app"></div>


//js Babel
/*props{
name:'Lara',
age:3,
}*/
//函式元件要大寫
function HelloMessage ( props ) {
return(
<div>
<h1>我是 { props.name } !</h1>
<p>我今年 { props.age } 歲</p>
</div>
)
}
const element = <HelloMessage name="Lara" age="3"/>;
const root = ReactDOM.createRoot(document.getElementById('root'));
//Rendar 只一次
root.render( element );


//return <div></div>包裹
//map() Key 綁定
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
function Welcome (props){
return <h1>{props.name},{props.age}</h1>
}


function App (){
return (
<div>
<Welcome name="lara" age="2"></Welcome>
<Welcome name="Tom" age="2"></Welcome>
</div>
)

}


const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<App/>);

React 環境建立cdn & JSX 表達式

環境建立(cdn):載入React(建立介面的資料結構),ReactDOM(將前者進行渲染)

1
2
3
4
5
6
7
8
9
10
11
12
13
//react 版本18
//以上的版本只適用於開發環境,並不適合用於線上環境。你可以在以下找到已壓縮和最佳化的 React 線上版本:
<script crossorigin src="https://unpkg.com/react@18/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>

//babel =>利用 Babel CDN 讓瀏覽器直接跑 JSX
<script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>

//官方版本 18,保留 crossorigin
<script crossorigin src="..."></script>
//六角讀書會教法
<script src="all" type=text/babel></script>

利用 Babel CDN 讓瀏覽器直接跑 JSX

JSX(javaScript and XML)

1
2
3
4
5
6
//陳述式Statement 
const a= 1;
//表達式 Expression
a
````
函式

//陳述式Statement
function b(){
return “Lara”
}
//表達式 Expression
b()

1
2


if(//表達式Expression){
let a=3;//陳述式Statement
}
if(2>1){
let a=3;
}



React Router

安裝 react-router-dom

目前最新版本6.8.1

1
npm install react-router-dom 

src/index.js加入
import { BrowserRouter } from ‘react-router-dom’

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
import { BrowserRouter } from 'react-router-dom'

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<BrowserRouter>
<App />
</BrowserRouter>
);

reportWebVitals();

pages資料內新增兩個檔案
FirstPage.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import React from 'react';

const FirstPage=()=>{
const StyleSheet={
width:"100vw",
height:"100vh",
backgroundColor:"#FF2E63",
display: "flex",
alignItems:"center",
justifyContent:"center",
flexDirection:"column"
}
return(
<div style={StyleSheet}>
<h1 style={{color:"white",fontFamily:"Microsoft JhengHei"}}>我是第一頁</h1>
</div>
)
}

export default FirstPage;

SecondPage.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import React from 'react';

const SecondPage=()=>{
const StyleSheet={
width:"100vw",
height:"100vh",
backgroundColor:"#08D9D6",
display: "flex",
alignItems:"center",
justifyContent:"center",
flexDirection:"column"
}
return(
<div style={StyleSheet}>
<h1 style={{color:"white",fontFamily:"Microsoft JhengHei"}}>我是第二頁</h1>
</div>
)
}

export default SecondPage;

App.js檔案如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import { Routes,Route,} from 'react-router-dom'
import './App.css';
import FirstPage from "./pages/FirstPage";
import SecondPage from "./pages/SecondPage";
function App() {
return (
<div className="App">
<h1>Home</h1>
<ul>
<li><a href='/'>Home</a></li>
<li><a href='/secondpage'>SecondPage</a></li>

</ul>
<Routes>
<Route index element={<FirstPage />} />
<Route path='secondpage' element={<SecondPage />} />
</Routes>
</div>
);
}
export default App;

React第一個專案

安裝 create-react-app

打開電腦裡的終端機來進行 create-react-app 的套件下載

1
npm install create-react-app

建置一個 React.js 的專案

在終端機輸入 cd 你的資料夾名稱

1
npx create-react-app 專案名稱

//cd 專案名稱
npm run start
預設網域在 localhost:3000

JSX 瀏覽器看不懂?要翻譯就靠 Babel

1
npm install @babel/runtime --save-dev

Javascript 目錄

功能 解析
js 三元運算子
charAt() 從一個字符串中返回指定的字符
concat() 合併兩個或多個數組
copyWrithin()拷貝索引
every() 是否全部都符合
flat() 創建一個新數組,其中所有子數組元素以遞歸方式連接到指定深度。
fill() 填充
findIndex() 尋找陣列中符合的元素,並返回其 index(索引)
find() 回傳第一個滿足測試函式的元素值
filter() 建立指定函式,由原陣列過濾指定函數產生新陣列
forEach() 迴圈的陣列
indexOf() 檢查某元素是否在陣列中
includes() 是否在其條目中包含某個值,返回true或 false
join() 連接數組
push() 將一個或多個元素加入數組
pop() 從數組刪除(移除)最後一個元素並返回該元素
map() 迴圈的陣列,產生一個新陣列
Math.abs() 基數的平方
Math.floor() 取得整數
Math.pow() 次方
Math.sprt() 平方
Math.max() 查詢最大值
Math.min() 查詢最小值
Math.random() 虛擬亂數
Math.round() 四捨五入
Math.ceil() 無條件進位
Math.trunc() 去掉小數部分
Math.imul() 乘法
Math.atan() 以弧度計算直角三角形的角度
Math.asin() 返回數字的Math.asin()反正弦(以弧度為單位)
Math.atan2() 靜態方法返回平面中正 x 軸與從 (0, 0) 到點 (x, y) 的射線之間的角度(以弧度為單位)
Math.acos() 返回數字的Math.acos()反餘弦值(以弧度為單位)
Math.cos() 返回以弧度為單位的數字的餘弦值
Math.tan() 返回以弧度為單位的數字的正切值
Math.sin() 三角函數sin
shift() 從數組刪除(移除)第一個元素並返回該元素
slice() 從數組中返回選定(擷取)的元素,作為一個新數組。
split() 分割
splice() 從數組移除或是取代數量
some() 至少一個符合
sort()排序
trim()清空左右兩邊空白
trimEnd()清空結束空白
trimStart()清空開始空白
toString()把陣列轉成字串
toLowerCase()把字符轉為英文小寫
toUpperCase()把字串轉換成英文大寫
reverse()反轉
reduce()相加
reduceRight()相減
replaceAll() 取代所有字符內相同數據
unshift() 從數組新增(添加)第一個元素並返回該元素