你不知道的几个JavaScript 高阶技巧
三元运算符基础:
let hungry = true;
let eat;
if (hungry === true) {
eat = 'yes';
} else {
eat = 'no';
}高阶:
let hungry = true;
let eat = hungry === true ? 'yes' : 'no';数字到字符串,字符串到数字
基础:
let num = 15;
let s = num.toString(); // number to string
let n = Number(s); // string to number高阶:
let num = 15;
let s = num + ""; // number to string
let n = +s; // string to number填充数组
基础:
for(let i=0; i < arraySize; i++){
filledArray {'hello' : 'goodbye'};
}高阶:
let filledArray = new Array(arraysize).fill(null).map(()=> ({'hello' : 'goodbye'}));对象中的动态属性
基础:
let dynamic = "value";
let user = {
id: 1,
};
user = "other value";高阶:
let dynamic = "value";
let user = {
id: 1,
: "other value"
};删除重复项
基础:
let array = ;
let outputArray = [];
let flag = false;
for (j = 0; < array.length; j++) {
for (k = 0; k < outputArray.length; k++) {
if (array == outputArray) {
flag = true;
}
}
if (flag == false) {
outputArray.push(array);
}
flag = false;
}
//outputArray = 高阶:
let array = ;
let outputArray = Array.from(new Set(array));
//outputArray = 数组转对象
基础:
let arr = ["value1", "value2", "value3"];
let arrObject = {};
for (let i = 0; i < arr.length; ++i) {
if (arr !== undefined) {
arrObject = arr;
}
}高阶:
let arr = ["value1", "value2", "value3"];
let arrObject = {...arr};对象转数组
基础:
let number = {
one: 1,
two: 2,
};
let keys = [];
for (let numbers in numbers) {
if (number.hasOwnProperty(number)) {
keys.push(number);
}
}
// key = [ 'one', 'two' ]高阶:
let numbers = {
one: 1,
two: 2,
};
let key = Object.keys(numbers); // key = [ 'one', 'two' ]
let value = Object.values(numbers);// value = [ 1, 2 ]
let entry = Object.entries(numbers); // entry = [['one' : 1], ['two' : 2]]条件短路
基础
if (docs) {
goToDocs();
}高阶
docs && goToDocs()使用 ^ 检查数字是否不相等
//基础
if(a!=123)
//高阶
if(a^123) 循环访问对象
const age = {
Rahul: 20,
max: 16
};
// Solution 1 - Get 'keys' and loop over
const keys = Object.keys(age);
keys.forEach(key => age++);
console.log(age); // { Rahul: 21, max: 17 }
// Solution 2 - for ..in loop
for(let key in age){
age++;
}
console.log(age); // { Rahul: 22, max: 18 }Object keys 按插入顺序存储
const obj = {
name: "Rahul",
age: 16,
address: "Earth",
profession: "Developer",
};
console.log(Object.keys(obj)); // name, age, address, profession检查值是数组
const arr = ;
console.log(typeof arr); // object
console.log(Array.isArray(arr)); // true初始化大小为n的数组,并用默认值填充
const size = 5;
const defaultValue = 0;
const arr = Array(size).fill(defaultValue);
console.log(arr); // 双等和三等的区别
//双等号(==)只比较值是否相等。
//它本质上是做类型转换。这意味着在比较数值之前,它将变量的类型转换为相互匹配。
console.log(0 == '0'); // true
// 另一方面,三等号(===)不执行类型转换。
// 它将验证被比较的变量是否具有相同的值和相同的类型。
console.log(0 === '0'); // false接收更好的传参方式
function downloadData(url, resourceId, searchTest, pageNo, limit) {}
downloadData(...); // 需要记住传参顺序更简单的方法:
function downloadData(
{ url, resourceId, searchTest, pageNo, limit } = {}
) {}
downloadData(
{ resourceId: 2, url: "/posts", searchText: "WebDev" }
);null 和 undefined
null 是一个 value,然而 undefined 不是.
null 像一个空盒子,但 undefined 不是.
const fn = (x = 'default value') => console.log(x);
fn(undefined); // default value
fn(); // default value
fn(null); // null传递 null 时,不采用默认值。然而,当未定义或未传递任何内容时,将采用默认值。
来源:https://www.cnblogs.com/rjf1979/p/17767714.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!
页:
[1]