Skip to content

when use key in Object.prototype carelessly as prop, it will work wrong way #11757

Open
@LongTengDao

Description

@LongTengDao

Version

2.6.12

Steps to reproduce

<div></div>
<script>
new Vue({
  el: 'main',
  template: '<MyDatetime :value="datetime" />',
  data: {
    datetime: Date.now(),
  },
  components: {
    MyDatetime: {
      template: `
        <div>
          expect undefined: {{ toString + "" }}<br />
          expect default: {{ toLocalString + "" }}<br />
        </div>`,
      props: {
        value: {
          type: Number,
        },
        toString: {
          validator: mode => [ 'date-time', 'date', 'time' ].includes(mode),
        },
        toLocalString: {
          validator: mode => [ 'date-time', 'date', 'time', 'default' ].includes(mode),
          default: 'default',
        },
      },
    },
  },
});
</script>

What is expected?

They are all keys match the uni simple rule: not start with _ or $

What is actually happening?

If parent component doesn't pass the prop, then the component will get neither undefined nor default, but the value in Object.prototype.


  1. {} in these two lines need to be replaced to Object.create(null), the first line is required for two cases, and the second line is required for default case,

const props = vm._props = {}

  1. there still leave constructor which can't be used, it need delete Vue.prototype.constructor, and give an alternative way to do what vm.constructor (Sub.prototype.constructor = Sub) do in current source code.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions