Skip to content

Commit

Permalink
fix: $set should respect properties on prototype chain
Browse files Browse the repository at this point in the history
fix #6845
  • Loading branch information
yyx990803 committed Oct 26, 2017
1 parent ee0e8b5 commit 83ed926
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/core/observer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ export function set (target: Array<any> | Object, key: any, val: any): any {
target.splice(key, 1, val)
return val
}
if (hasOwn(target, key)) {
if (key in target && !(key in Object.prototype)) {
target[key] = val
return val
}
Expand Down
26 changes: 26 additions & 0 deletions test/unit/features/global-api/set-delete.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,32 @@ describe('Global API: set/delete', () => {
expect(vm.$el.innerHTML).toBe('<p>D</p><p>B</p><p>C</p>')
}).then(done)
})

// #6845
it('should not overwrite properties on prototype chain', () => {
class Model {
constructor () {
this._bar = null
}
get bar () {
return this._bar
}
set bar (newvalue) {
this._bar = newvalue
}
}

const vm = new Vue({
data: {
data: new Model()
}
})

Vue.set(vm.data, 'bar', 123)
expect(vm.data.bar).toBe(123)
expect(vm.data.hasOwnProperty('bar')).toBe(false)
expect(vm.data._bar).toBe(123)
})
})

describe('Vue.delete', () => {
Expand Down

0 comments on commit 83ed926

Please sign in to comment.