diff --git a/src/core/instance/state.js b/src/core/instance/state.js index cc1401a3353..a73d4cdd434 100644 --- a/src/core/instance/state.js +++ b/src/core/instance/state.js @@ -269,6 +269,11 @@ function initMethods (vm: Component, methods: Object) { `Did you reference the function correctly?`, vm ) + } else if (typeof methods[key] !== 'function') { + warn( + `Method "${key}" should be a function.` + ) + methods[key] = null } if (props && hasOwn(props, key)) { warn( diff --git a/test/unit/features/options/methods.spec.js b/test/unit/features/options/methods.spec.js index f1e5fc68b27..4def07f1a9b 100644 --- a/test/unit/features/options/methods.spec.js +++ b/test/unit/features/options/methods.spec.js @@ -37,7 +37,7 @@ describe('Options methods', () => { foo () {} } }) - expect(`Method "foo" has already been defined as a data property`).toHaveBeenWarned() + expect('Method "foo" has already been defined as a data property').toHaveBeenWarned() }) it('should warn methods conflicting with internal methods', () => { @@ -46,6 +46,15 @@ describe('Options methods', () => { _update () {} } }) - expect(`Method "_update" conflicts with an existing Vue instance method`).toHaveBeenWarned() + expect('Method "_update" conflicts with an existing Vue instance method').toHaveBeenWarned() + }) + + it('should warn if method is not a function', () => { + new Vue({ + methods: { + notAFunction: {} + } + }) + expect('Method "notAFunction" should be a function').toHaveBeenWarned() }) })