前端那些不经常用却需要记住的方法

217 阅读2分钟

Vue

// 用loadsh检查data内的数据是否做过修改
this.getPlugin('lodash').isEqual(this.$data, this.$options.data.apply(this))

// 将this绑定的值还原到初始状态
Object.assign(this, this.$options.data())

// 数组内字符串转数字
['1','2','3'].map(Number)

React

// 使用eslint需要的包
yarn add eslint
yarn add babel-eslint               // eES2015 及以上
yarn add eslint-config-airbnb       // 配置一些 eslint rules 的规范
yarn add eslint-plugin-import       // 在使用 import 的时候,一些 rules 规范
yarn add eslint-plugin-react        // 一些 react 的 eslint 的 rules 规范
yarn add eslint-plugin-jsx-a11y     // 一些 jsx 的 rules 规范

// 创建基础的 .eslintrc.js
yarn eslint --init

JS

// ES6 循环
Array.apply(null, { length: 20 }).forEach(() => {
    // do someting
})


// 数组内字符串转数字
['1','2','3'].map(Number)

//  检测设备类型(pc or mobile)
(function() {
  var url = location.href;
  // replace www.test.com with your domain
  if ((url.indexOf('www.test.com') != -1) && navigator.userAgent.match(/(iPhone|iPod|Android|ios|iPad)/i)) {
    var newUrl = url.replace('http://www', 'http://m');
    location.href = newUrl;
  }
})();

node

// 根据在 package.json 文件中所指定的版本范围将依赖更新到其最新版本
// --latest : 忽略在 package.json 中指定的版本范围
yarn upgrade
yarn upgrade-interactive

HTTP状态码

100  Continue  继续,一般在发送post请求时,已发送了http header之后服务端将返回此信息,表示确认,之后发送具体参数信息
200  OK   正常返回信息
201  Created  请求成功并且服务器创建了新的资源
202  Accepted  服务器已接受请求,但尚未处理
301  Moved Permanently  请求的网页已永久移动到新位置。
302 Found  临时性重定向。
303 See Other  临时性重定向,且总是使用 GET 请求新的 URI。
304  Not Modified  自从上次请求后,请求的网页未修改过。

400 Bad Request  服务器无法理解请求的格式,客户端不应当尝试再次使用相同的内容发起请求。
401 Unauthorized  请求未授权。
403 Forbidden  禁止访问。
404 Not Found  找不到如何与 URI 相匹配的资源。

500 Internal Server Error  最常见的服务器端错误。
503 Service Unavailable 服务器端暂时无法处理请求(可能是过载或维护)。

Http

  • 运行在TCP之上
  • 本质上是Web前端程序和Web后端程序的通信的协议
  • 指定了客户端与服务端交流所用的格式

RESTful

  • REST只是一种风格,不是一种标准
  • REST充分利用和依赖HTTP协议
  • 显式地使用不同的 HTTP 请求方法
  • 动作类型为新增、变更、删除所调用资源