Javascript - fantasy0107/notes GitHub Wiki

Javascript

array method

// forEach 跑每一個item 
array1.forEach(function(element, index) {
  console.log(element);
});

if 中 false

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.

JS ES6 (未完成)

ES6 cheat sheet

變數
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()

Template Literals

可以傳變數 用 `` + ${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) => {}

JSX property

<Button
    onclikc={() => {}}
/>

物件

Destructuring

取得 物件 property

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

// spread conditional
return {
    guid: id,
    title,
    summary: description,
    ...(cond1 && {optionalField1 : 1, optionalField2 : 2, optionalField3 : 3},

}

modules

// 單一
import { greet } from './formalities';

//全部
import greet from './formalities';

import / export

// 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();

參考資料

A Brief Overview of ES6 for React Native Developers

⚠️ **GitHub.com Fallback** ⚠️