Skip to content

Commit

Permalink
fix vuejs#4041, warn overriding Vue's internal methods
Browse files Browse the repository at this point in the history
  • Loading branch information
HerringtonDarkholme committed Nov 4, 2016
1 parent 35f145c commit 3bbe952
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/core/instance/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import {
noop
} from '../util/index'

import BuiltinVue from '../index'

export function initState (vm: Component) {
vm._watchers = []
initProps(vm)
Expand Down Expand Up @@ -143,12 +145,17 @@ function initMethods (vm: Component) {
if (methods) {
for (const key in methods) {
vm[key] = methods[key] == null ? noop : bind(methods[key], vm)
if (process.env.NODE_ENV !== 'production' && methods[key] == null) {
warn(
if (process.env.NODE_ENV !== 'production') {
methods[key] == null && warn(
`method "${key}" has an undefined value in the component definition. ` +
`Did you reference the function correctly?`,
vm
)
hasOwn(BuiltinVue.prototype, key) && warn(
`You're overriding Vue's internal method "${key}". ` +
`Beware of misbehaviors.`,
vm
)
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions test/unit/features/options/methods.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,14 @@ describe('Options methods', () => {
})
expect(`method "hello" has an undefined value in the component definition`).toHaveBeenWarned()
})

it('should warn overriding builtin methods', () => {
new Vue({
methods: {
$emit () {
}
}
})
expect(`You're overriding Vue's internal method "$emit". Beware of misbehaviors.`).toHaveBeenWarned()
})
})

0 comments on commit 3bbe952

Please sign in to comment.