Javascript snippets
- ?? operator will coerce undefined and null values to false and other values to true.
let propertyCheck = person.age ?? 0;
let objectAndPropertyCheck = (person ?? {}).age ?? 0;
// or
let objectAndPropertyCheck2 = person?.age ?? 0;
// the ? character will stop evaluating an expression if the value
// it is applied to is null or undefined.
- select properties
let person = {
name: 'psi',
age: 20,
}
let { name, ...someProperties } = person;
console.log(`Selected: ${JSON.stringify(someProperties)}`);
// Selected: {"age":20}
- this
// node环境下,最外层的对象是global,但最外层的this并不是全局对象global,
// 而是module.exports
console.log(module.exports === this); //true
// 但从最外层调用函数,将会使得函数中的this指向global。
//浏览器环境下,最外层的对象是windows,并且最外层的this就是windows。