proxy.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /* not type checking this file because flow doesn't play well with Proxy */
  2. import { warn, makeMap } from '../util/index'
  3. let hasProxy, proxyHandlers, initProxy
  4. if (process.env.NODE_ENV !== 'production') {
  5. const allowedGlobals = makeMap(
  6. 'Infinity,undefined,NaN,isFinite,isNaN,' +
  7. 'parseFloat,parseInt,decodeURI,decodeURIComponent,encodeURI,encodeURIComponent,' +
  8. 'Math,Number,Date,Array,Object,Boolean,String,RegExp,Map,Set,JSON,Intl,' +
  9. 'require,__webpack_require__' // for Webpack/Browserify
  10. )
  11. hasProxy =
  12. typeof Proxy !== 'undefined' &&
  13. Proxy.toString().match(/native code/)
  14. proxyHandlers = {
  15. has (target, key) {
  16. const has = key in target
  17. const isAllowedGlobal = allowedGlobals(key)
  18. if (!has && !isAllowedGlobal) {
  19. warn(
  20. `Property or method "${key}" is not defined on the instance but ` +
  21. `referenced during render. Make sure to declare reactive data ` +
  22. `properties in the data option.`,
  23. target
  24. )
  25. }
  26. return !isAllowedGlobal
  27. }
  28. }
  29. initProxy = function initProxy (vm) {
  30. if (hasProxy) {
  31. vm._renderProxy = new Proxy(vm, proxyHandlers)
  32. } else {
  33. vm._renderProxy = vm
  34. }
  35. }
  36. }
  37. export { initProxy }