Javascript - fantasy0107/notes GitHub Wiki
// forEach 跑每一個item
array1.forEach(function(element, index) {
console.log(element);
});
Undefined false
Null false
Boolean The result equals the input argument (no conversion).
Number The result is false if the argument is +0, −0, or NaN;
otherwise the result is true.
String The result is false if the argument is the empty
String (its length is zero); otherwise the result is true.
Object true.
變數
var - function 內呼叫不到如果沒有設定的話
let & const - function 內可被呼叫到 (block scoped)
var snack = 'Meow Mix';
function getFood(food) {
if (food) {
var snack = 'Friskies';
return snack;
}
return snack;
}
getFood(false); // undefined
//=================================
let snack = 'Meow Mix';
function getFood(food) {
if (food) {
let snack = 'Friskies';
return snack;
}
return snack;
}
getFood(false); // 'Meow Mix'
ES6 可以建立 block-based scopes
preserve the context of this from its lexical scope
字串
.includes()
.repeat()
可以傳變數 用 `` + ${variableName}
const greet = (name, company) => {
// return 'Hello, ' + name + '!' + 'How is ' + company + '?';
return `Hello, ${name}! How is ${company}?`;
};
將 array 或 object 存進變數
ES6 可以直接使用 modules
import - 引入 / export - 輸出
經常使用export default
在 module 的末端
參數
{
data && (
<TouchableOpacity>
進入點
</TouchableOpacity>
)
}
test ? expression1 : expression2
A => {}
//無 {}
const greet = (name) => 'Hello, ' + name + '!';
greet('Spencer'); // Hello, Spencer!
//回傳 object
const getInfo = () => ({
name: 'Spencer',
company: 'Handlebar Labs',
job: 'Teaching',
});
getInfo(); // { name: 'Spencer', company: 'Handlebar Labs', job: 'Teaching' }
//回傳 component
const Greeting = ({ name }) => (
<View>
<Text>Hello, {name}!</Text>
</View>
);
() => {}
(A, B, C) => {}
<Button
onclikc={() => {}}
/>
const info = {
name: 'Spencer',
company: 'Handlebar Labs',
location: {
city: 'Nashville',
state: 'Tennessee',
},
};
//一般
const name = info.name;
const city = info.location.city;
const state = info.location.state;
//ES6
const { name, location } = info;
const { city, state } = location;
// name is Spencer
// city is Nashville
// state is Tennessee
//from props
const Info = ({ name, location }) => (
<View>
<Text>{name} lives in {location.city}, {location.state}</Text>
</View>
);
// spread conditional
return {
guid: id,
title,
summary: description,
...(cond1 && {optionalField1 : 1, optionalField2 : 2, optionalField3 : 3},
}
// 單一
import { greet } from './formalities';
//全部
import greet from './formalities';
// Name exports
import { square, diag } from 'lib';
import * as lib from 'lib';
console.log(square(11)); // 121
console.log(lib.square(11)); // 121
// Default exports
export default function () { ... };
import myFunc from 'myFunc';
myFunc();