function titleBigCase(str) {
return str.toLowerCase().replace(/(?:^|_)[a-z]/g, function(s) {
return s.toUpperCase().replace(/_/g, '');
});
};
console.log(titleBigCase('property_id'));
复制代码
function titleCase(str) {
return str.toLowerCase().replace(/(?:_)[a-z]/g, function(s) {
return s.toUpperCase().replace(/_/g, '');
});
};
console.log(titleCase('property_id'));
复制代码
var s = "propertyId";
s = s.replace(/([A-Z])/g, "_$1").toLowerCase();
console.log(s);
复制代码
function titleCase(str) {
return str.replace(/( |^)[a-z]/g, L => L.toUpperCase());
}
复制代码
const required = () => { throw new Error('Missing parameter'); };
const add = (a = required(), b = required()) => a + b;
add(1, 2);
add(1);
复制代码
const numbers = [10, 20, 30, 40];
const doubledOver50 = numbers.reduce((finalList, num) => {
num *= 2;
if (num > 50) {
finalList.push(num);
}
return finalList;
}, []);
复制代码
const cars = ['BMW', 'Benz', 'Benz', 'Tesla', 'BMW', 'Toyota'];
const carsObj = cars.reduce((obj, name) => {
obj[name] = obj[name] ? obj[name] += 1 : 1;
return obj;
}, {});
console.log(carsObj);
复制代码
const { _internal, tooBig, ...cleanObject } = {
el1: '1', _internal: 'secret', tooBig: {}, el2: '2', el3: '3',
};
console.log(cleanObject);
复制代码
const car = {
model: 'bmw 2018',
engine: {
v6: true,
turbo: true,
vin: 12345,
},
};
const modelAndVIN = ({ model, engine: { vin } }) => ({ model, vin });
console.log(modelAndVIN(car));
复制代码
function concatenateAll(...args) {
return args.join('');
}
复制代码
const a = {};
a.x = 3;
const a = { x: null };
a.x = 3;
const a = {};
Object.assign(a, { x: 3 });
复制代码
函数的参数如果是对象的成员,优先使用解构赋值。
function getFullName(user) {
const firstName = user.firstName;
const lastName = user.lastName;
}
function getFullName({ firstName, lastName }) {
}
如果函数返回多个值,优先使用对象的解构赋值,而不是数组的解构赋值。这样便于以后添加返回值,以及更改返回值的顺序。
function processInput(input) {
return [left, right, top, bottom];
}
function processInput(input) {
return { left, right, top, bottom };
}
const { left, right } = processInput(input);
复制代码
function foo(args) {
return args.reduce((prev, arg) => prev.then(() => bar(arg), Promise.resolve());
}
复制代码