Skip to content

Commit

Permalink
fix #4872, use context agnostic Function constructor check (#4928)
Browse files Browse the repository at this point in the history
* fix #4872, use context agnostic Function constructor check

* use getType to check Function Constructor

* fix negation
  • Loading branch information
HerringtonDarkholme authored and yyx990803 committed Feb 14, 2017
1 parent dfaf126 commit 01151ce
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/core/util/props.js
Expand Up @@ -70,7 +70,8 @@ function getPropDefaultValue (vm: ?Component, prop: PropOptions, key: string): a
return vm._props[key]
}
// call factory function for non-Function types
return typeof def === 'function' && prop.type !== Function
// a value is Function if its prototype is function even across different execution context
return typeof def === 'function' && getType(prop.type) !== 'Function'
? def.call(vm)
: def
}
Expand Down
18 changes: 18 additions & 0 deletions test/ssr/ssr-string.spec.js
@@ -1,4 +1,5 @@
import Vue from '../../dist/vue.runtime.common.js'
import VM from 'vm'
import { createRenderer } from '../../packages/vue-server-renderer'
const { renderToString } = createRenderer()

Expand Down Expand Up @@ -699,6 +700,23 @@ describe('SSR: renderToString', () => {
done()
}, context)
})

it('default value Foreign Function', () => {
const FunctionConstructor = VM.runInNewContext('Function')
const func = () => 123
const vm = new Vue({
props: {
a: {
type: FunctionConstructor,
default: func
}
},
propsData: {
a: undefined
}
})
expect(vm.a).toBe(func)
})
})

function renderVmWithOptions (options, cb) {
Expand Down
16 changes: 16 additions & 0 deletions test/unit/features/options/props.spec.js
Expand Up @@ -98,6 +98,22 @@ describe('Options props', () => {
}).then(done)
})

it('default value Function', () => {
const func = () => 132
const vm = new Vue({
props: {
a: {
type: Function,
default: func
}
},
propsData: {
a: undefined
}
})
expect(vm.a).toBe(func)
})

it('warn object/array default values', () => {
new Vue({
props: {
Expand Down

0 comments on commit 01151ce

Please sign in to comment.