使用hasOwnProperty出现警告原因
ESLint使用了禁止直接调用 Object.prototypes 的内置属性开关(ESLint 配置文件中的 "extends": "eslint:recommended") 属性启用了此规则。Object.create,它支持使用指定的原型创建对象。Object.create(null) 是一种常见的模式,用于创建将用作映射的对象。
- 此对象调用Object.prototype 的属性时,这可能会导致错误。该规则防止直接调用某些 Object.prototype 的方法。
- 此外,对象可以具有属性,这些属性可以将 Object.prototype 的内建函数隐藏,可能导致意外行为或拒绝服务安全漏洞。 例如,web 服务器解析来自客户机的 JSON 输入并直接在结果对象上调用 hasOwnProperty 是不安全的,因为恶意客户机可能发送一个JSON值,如 {"hasOwnProperty": 1},并导致服务器崩溃。
let obj = Object.create(null) // undefined
obj.hasOwnProperty() // error!
使用apply改变指向,总是在 Object.prototype 调用这些方法
Object.prototype.hasOwnProperty.call(foo, "bar")
自动化全局注册
在开发vue项目过程中,如组件等过多的话,注册起来是一件很麻烦的事情,每个用到的页面都要引入一次, 要是能全局引用,每个页面可自行使用那将是很愉快的事情。
//main.js
//自动化注册全局组件
const requireComponent = require.context('./components/global', true, /\.vue$/);
requireComponent.keys().forEach(fileName => {
// 获取组件配置
const componentConfig = requireComponent(fileName);
// 剥去文件名开头的 `./` 和`.vue`结尾的扩展名
const componentName = fileName.replace(/^\.\//,'').replace(/\.vue$/,'');
// 全局注册组件
const component = Vue.component(
componentName.replace(/\//,'-'),
componentConfig.default || componentConfig
);
});