JS 相關筆記
By call method which return from function to change local variable.
藉由呼叫函數中回傳的方法變更區域變數。
Ref:
被借用的方法.call(目標, ...參數) => 目標.被借用的方法(...參數)
被借用的方法.apply(目標, [...參數]) => 目標.被借用的方法(...參數)
var arr = [];
Array.prototype.push.apply(arr, ["a", "b", "c", "d"]); // arr.push("a", "b", "c", "d")
console.log(arr); // Result: ["a", "b", "c", "d"]
String.prototype.split.call("1,2,3,4", ","); // "1,2,3,4".split(",")
// Result: ["1", "2", "3", "4"]
方法.bind(目標) => 方法可存取目標物件所定義之成員
class C extends React.Component {
constructor() {
this.onMouseClick = this.onMouseClick.bind(this);
}
onMouseClick() {
console.log("mouse click")
}
render() {
<div onClick={this.onMouseClick}>TEST</div>
}
}
Difference between '==' and '===':
Former has no type comparison, latter has.
typeof /regexp/ // "object"
typeof [] // "object"
typeof {} // "object"
Based on your knowledge of var hoisting, and write the code below:
a = 1;
var a;
List two or three hack tricks to deal with different browsers:
Selector, property, value, javascript, css and html hacks.
Ref: Browser Hacks
What's the difference between pseudo element and class?
Write down at least two modular javascript style:
RequireJS (AMD)
define(['jquery', 'underscore'], function($, _) {...}
CommonJS
// foobar.js
export default const foo = (str) => console.log(str)
export const bar = "Hello, World!";
// main.js
import foo, { bar } from "foobar";
foo(bar); // "Hello, World!"
What's the difference between cookies, localStorage and sessionStorage?
/* Range of date display by moment. */
// yesterday
startDate.add(-1, "days")
endDate.add(-1, "days")
// today
moment()
moment()
// tomorrow
startDate.add(1, "days")
endDate.add(1, "days")
// previous week
startDate.weekday(-7)
endDate.weekday(-1)
// this week
moment().weekday(0)
moment().weekday(6)
// previous month
startDate.startOf("month").add(-1, "months")
endDate.endOf("month").add(-1, "months")
// this month
moment().startOf("month")
moment().endOf("month")
// display weekday
const weekdays = ["日", "一", "二", "三", "四", "五", "六"]
const weekday = i => `星期${weekdays[i]}`;
weekday(moment(date).format("d")); // ex. 星期日