fix #4872, use context agnostic Function constructor check (#4928)

* fix #4872, use context agnostic Function constructor check

* use getType to check Function Constructor

* fix negation
This commit is contained in:
Herrington Darkholme 2017-02-15 02:40:57 +08:00 committed by Evan You
parent dfaf1265a2
commit 01151ce3fa
3 changed files with 36 additions and 1 deletions

View File

@ -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
}

View File

@ -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()
@ -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) {

View File

@ -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: {