[关闭]
@Secretmm 2021-01-21T02:30:25.000000Z 字数 3665 阅读 456

js/ts语言规范

梳理

1. 变量声明

  1. // 推荐
  2. const a = 1
  3. let b = 2
  4. // 不推荐
  5. var a = 1
  6. var b = 2
  1. const b = 1;
  2. const d = 2;
  3. let a;
  4. let c;
  5. let e;
  1. // 不推荐
  2. let a = b = c = 1;

2. 对象

  1. // 推荐
  2. const a = {};
  3. // 不推荐
  4. const a = new Object();
  1. // 推荐
  2. const item = {
  3. value: 1,
  4. addValue(val) {
  5. return item.value + val;
  6. },
  7. };
  8. // 不推荐
  9. const item = {
  10. value: 1,
  11. addValue: function(val) {
  12. return item.value + val;
  13. },
  14. };
  1. const job = 'FrontEnd';
  2. // 推荐
  3. const item = {
  4. job,
  5. };
  6. // 不推荐
  7. const item = {
  8. job: job,
  9. };
  1. const job = 'FrontEnd';
  2. const company = 'daddylab';
  3. // 推荐
  4. const item = {
  5. job,
  6. company,
  7. sex: 'male',
  8. age: 25,
  9. };
  10. // 不推荐
  11. const item = {
  12. sex: 'male',
  13. job,
  14. age: 25,
  15. company,
  16. };
  1. // 推荐
  2. const good = {
  3. foo: 3,
  4. bar: 4,
  5. 'data-blah': 5,
  6. };
  7. // 不推荐
  8. const bad = {
  9. 'foo': 3,
  10. 'bar': 4,
  11. 'data-blah': 5,
  12. };
  1. // 推荐
  2. const original = { a: 1, b: 2 };
  3. const copy = { ...original, c: 3 }; // copy => { a: 1, b: 2, c: 3 }
  4. // 不推荐
  5. const original = { a: 1, b: 2 };
  6. const copy = Object.assign({}, original, { c: 3 }); // copy => { a: 1, b: 2, c: 3 }

3. 数组

  1. // 推荐
  2. const items = [];
  3. // 不推荐
  4. const items = new Array();
  1. const items = [];
  2. // 推荐
  3. items.push('test');
  4. // 不推荐
  5. items[items.length] = 'test';
  1. // 推荐
  2. const itemsCopy = [...items];
  3. // 不推荐
  4. const itemsCopy = [];
  5. for (i = 0; i < items.length; i++) {
  6. itemsCopy[i] = items[i];
  7. }
  1. // 推荐
  2. const arr = [[0, 1], [2, 3], [4, 5]];
  3. const arr2 = [
  4. [0, 1],
  5. [2, 3],
  6. [4, 5]
  7. ];
  8. const objectInArray = [
  9. {
  10. id: 1,
  11. },
  12. {
  13. id: 2,
  14. }
  15. ];
  16. const numberInArray = [
  17. 1,
  18. 2
  19. ];
  20. // 不推荐
  21. const arr = [
  22. [0, 1], [2, 3], [4, 5]
  23. ];
  24. const objectInArray = [{
  25. id: 1,
  26. }, {
  27. id: 2,
  28. }];
  29. const numberInArray = [
  30. 1, 2
  31. ];

4. 解构赋值

  1. // 推荐
  2. function getFullName(user) {
  3. const { firstName, lastName } = user;
  4. return `${firstName} ${lastName}`;
  5. }
  6. // 很推荐
  7. function getFullName({ firstName, lastName }) {
  8. return `${firstName} ${lastName}`;
  9. }
  10. // 不推荐
  11. function getFullName(user) {
  12. const firstName = user.firstName;
  13. const lastName = user.lastName;
  14. return `${firstName} ${lastName}`;
  15. }
  1. const arr = [1, 2, 3, 4];
  2. // 推荐
  3. const [first, second] = arr;
  4. // 不推荐
  5. const first = arr[0];
  6. const second = arr[1];
  1. // 推荐
  2. function doSomething() {
  3. return { top, right, bottom, left };
  4. }
  5. const { top, left } = doSomething();
  6. // 不推荐
  7. function doSomething() {
  8. return [top, right, bottom, left];
  9. }
  10. const [top, xx, xxx, left] = doSomething();

5. 字符串

  1. const test = 'test';
  2. // 推荐
  3. const str = `ab${test}`;
  4. // 不推荐
  5. const str = 'a' + 'b' + test;

6. 函数

  1. (function() {
  2. // do something
  3. }());
  1. // 推荐
  2. function test(...args) {
  3. return args.join('');
  4. }
  5. // 不推荐
  6. function test() {
  7. const args = Array.prototype.slice.call(arguments);
  8. return args.join('');
  9. }
  1. // 推荐
  2. function handleThings(opts = {}) {
  3. // ...
  4. }
  5. // 不推荐
  6. function handleThings(opts) {
  7. opts = opts || {};
  8. // ...
  9. }
  1. let b = 1
  2. // 不推荐
  3. function count(a = b++) {
  4. console.log(a);
  5. }
  1. // 推荐
  2. function handleThings(name, opts = {}) {
  3. // ...
  4. }
  5. // 不推荐
  6. function handleThings(opts = {}, name) {
  7. // ...
  8. }
  1. // 不推荐
  2. function f1(obj) {
  3. obj.key = 1;
  4. }
  5. function f1(a) {
  6. a = 1;
  7. }

7. 箭头函数

  1. // 推荐
  2. [1, 2, 3].map((x) => {
  3. const y = x + 1;
  4. return x * y;
  5. });
  6. // 不推荐
  7. [1, 2, 3].map(function(x) {
  8. const y = x + 1;
  9. return x * y;
  10. });
  1. // 推荐
  2. [1, 2, 3].map(number => `A string containing the ${number}.`);
  1. // 推荐
  2. [1, 2, 3].map(x => x * x);
  3. [1, 2, 3].map((x) => {
  4. const y = x + 1;
  5. return x * y;
  6. });

8. 类&构造函数

  1. // 不推荐
  2. class Foo {
  3. bar () { return 1 }
  4. bar () { return 2 }
  5. }

9. 使用Map结构

区分ObjectMap,只有模拟现实世界的实体对象时,才使用Object。如果只是需要key: value的数据结构,使用Map结构

10. 模块

  1. // 推荐
  2. import foo, {
  3. named1,
  4. named2
  5. } from 'foo';
  6. // 不推荐
  7. import foo from 'foo';
  8. // … some other imports … //
  9. import { named1, named2 } from 'foo';

11. 对象属性

  1. // 推荐
  2. const name = joke.name;
  3. function getProp (prop) {
  4. return joke[prop];
  5. }
  6. // 不推荐
  7. const name = joke['name'];

12. 比较运算符

使用===!==而非==!=

13. eval()

一般情况禁用该方法

14.修改内置对象的原型

禁止修改内置对象,如ObjectArray

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注