Tuesday, January 19, 2021

Syntax และ Shorthand Techniquesในภาษา JavaScript

 สำหรับภาษา JavaScript เชื่อว่าเป็นภาษาที่หลายคนคุ้นเคยกันมาอย่างช้านาน เนื่องจากเป็นภาษาที่ใช้สำหรับเพิ่มลูกเล่นให้กับเว็บไซต์ และบทความนี้จะมาแนะนำ Syntax และวิธีการเขียนโค้ดให้สั้นและกระชัด แบบจัดเต็ม มีอะไรบ้างนั้นไปดูกันเล๊ยยยย

?: (Ternary Operator)

bool isLogin = false;String message = "";if(isLogin) {
message = "Welcome";
} else {
message = "Please login";
}
bool isLogin = false;
String message = isLogin ? "Welcome" : "Please login";

|| (Short-circuit Evaluation)

if (var1 !== null || var1 !== undefined || var1 !== '') {
let var2 = var1;
}
const var2 = var1  || 'default value';

=> (Arrow notation)

function lowerCase(String str) {
return str.toLowerCase();
}
const lowerCase = str =>  str.toLowerCase();

+ (Converting a String into a Number)

let age = parseInt('25');
let age = +'25';

` (Template Literals)

const welcome = 'You have logged in as ' + first + ' ' + last + '.'

const db = 'http://' + host + ':' + port + '/' + database;
const welcome = `You have logged in as ${first} ${last}`;

const db = `http://${host}:${port}/${database}`;

Declaring variables

let x;
let y;
let z = 3;
let x, y, z=3;

If Presence

if (likeJavaScript === true)
if (likeJavaScript !== true)
if (likeJavaScript)
if (!likeJavaScript)

Decimal Base Exponents

for (let i = 0; i < 10000; i++) {}
for (let i = 0; i < 1e7; i++) {}

// All the below will evaluate to true
1e0 === 1;
1e1 === 10;
1e2 === 100;
1e3 === 1000;
1e4 === 10000;
1e5 === 100000;

Default Parameter Values

function volume(l, w, h) {
if (w === undefined)
w = 3;
if (h === undefined)
h = 4;
return l * w * h;
}
volume = (l, w = 3, h = 4 ) => (l * w * h);

volume(2) //output: 24

JavaScript For Loop

const fruits = ['mango', 'peach', 'banana'];
for (let i = 0; i < fruits.length; i++)
for (let fruit of fruits)
for (let index in fruits)

… (Spread operator)

// joining arrays
const odd = [1, 3, 5];
const nums = [2 ,4 , 6].concat(odd);

// cloning arrays
const arr = [1, 2, 3, 4];
const arr2 = arr.slice()
// joining arrays
const odd = [1, 3, 5 ];
const nums = [2 ,4 , 6, ...odd];
console.log(nums); // [ 2, 4, 6, 1, 3, 5 ]

// cloning arrays
const arr = [1, 2, 3, 4];
const arr2 = [...arr];
const odd = [1, 3, 5 ];
const nums = [2, ...odd, 4 , 6];
const { a, b, ...z } = { a: 1, b: 2, c: 3, d: 4 };
console.log(a) // 1
console.log(b) // 2
console.log(z) // { c: 3, d: 4 }

Destructuring Assignment

const observable = require('mobx/observable');
const action = require('mobx/action');
const runInAction = require('mobx/runInAction');

const store = this.props.store;
const form = this.props.form;
const loading = this.props.loading;
const errors = this.props.errors;
const entity = this.props.entity;
import { observable, action, runInAction } from 'mobx';

const { store, form, loading, errors, entity } = this.props;
const { store, form, loading, errors, entity:customVariable } = this.props;

Multi-line String

const profile = 'I am NottDev.\n\t'
+ 'I am 25 years old.\n\t'
+ 'My hobby is blogging.\n\t';
const profile = `I am NottDev.
I am 25 years old.
My hobby is blogging.`;

~~ (Double Bitwise NOT)

Math.floor(4.9) === 4  //true
~~4.9 === 4  //true

** (Exponent Power)

Math.pow(2,3); // 8
Math.pow(2,2); // 4
Math.pow(4,3); // 64
2**3 // 8
2**4 // 4
4**3 // 64

~ (Bitwise IndexOf)

if(arr.indexOf(item) > -1) { // Confirm item IS found

}

if(arr.indexOf(item) === -1) { // Confirm item IS NOT found

}
if(~arr.indexOf(item)) { // Confirm item IS found

}

if(!~arr.indexOf(item)) { // Confirm item IS NOT found

}

Object Property

const x = 1920, y = 1080;
const obj = { x:x, y:y };
const obj = { x, y };

Object.entries()

const credits = { producer: 'John', director: 'Jane', assistant: 'Peter' };
const arr = Object.entries(credits);
console.log(arr);

/** Output:
[ [ 'producer', 'John' ],
[ 'director', 'Jane' ],
[ 'assistant', 'Peter' ]
]
**/

Object.values()

const credits = { producer: 'John', director: 'Jane', assistant: 'Peter' };
const arr = Object.values(credits);
console.log(arr);

/** Output:
[ 'John', 'Jane', 'Peter' ]
**/

No comments:

Post a Comment