We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
// Bad: const yyyymmdstr = moment().format("YYYY/MM/DD"); // Good: const currentDate = moment().format("YYYY/MM/DD");
// Bad: getUserInfo(); getClientData(); getCustomerRecord(); // Good: getUser();
// Bad: setTimeout(blastOff, 86400000); // Good: const MILLISECONDS_IN_A_DAY = 86400000; setTimeout(blastOff, MILLISECONDS_IN_A_DAY);
// Bad: const address = "One Infinite Loop, Cupertino 95014"; const cityZipCodeRegex = /^[^,\\]+[,\\\s]+(.+?)\s*(\d{5})?$/; saveCityZipCode( address.match(cityZipCodeRegex)[1], address.match(cityZipCodeRegex)[2] ); // Good: const address = "One Infinite Loop, Cupertino 95014"; const cityZipCodeRegex = /^[^,\\]+[,\\\s]+(.+?)\s*(\d{5})?$/; const [, city, zipCode] = address.match(cityZipCodeRegex) || []; saveCityZipCode(city, zipCode);
显式胜于隐式
// Bad: const locations = ["Austin", "New York", "San Francisco"]; locations.forEach(l => { doStuff(); doSomeOtherStuff(); // ... // ... // ... // l 是个什么? dispatch(l); }); // Good: const locations = ["Austin", "New York", "San Francisco"]; locations.forEach(location => { doStuff(); doSomeOtherStuff(); // ... // ... // ... dispatch(location); });
如果你的类或者对象名称告诉您某些内容,请不要在变量名称中重复。
// Bad: const Car = { carMake: "Honda", carModel: "Accord", carColor: "Blue" }; function paintCar(car) { car.carColor = "Red"; } // Good: const Car = { make: "Honda", model: "Accord", color: "Blue" }; function paintCar(car) { car.color = "Red"; }
默认参数通常比判断更干净。请注意,如果使用它们,则函数将仅提供未定义参数的默认值。其他“虚假”值(例如”,“”,false,null,0和NaN)将不会替换为默认值。
// Bad: function createMicrobrewery(name) { const breweryName = name || "Hipster Brew Co."; // ... } // Good: function createMicrobrewery(name = "Hipster Brew Co.") { // ... }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
JavaScript变量代码简洁可读性
使用有意义且明显的变量名
对相同类型的变量使用相同的词汇表
使用可搜索的名称
使用解释变量
避免没有语义化的行为
显式胜于隐式
不要添加不需要的上下文
如果你的类或者对象名称告诉您某些内容,请不要在变量名称中重复。
使用默认参数代替有条件的判断
默认参数通常比判断更干净。请注意,如果使用它们,则函数将仅提供未定义参数的默认值。其他“虚假”值(例如”,“”,false,null,0和NaN)将不会替换为默认值。
The text was updated successfully, but these errors were encountered: