From 01151ce3fa3cb346443d086332ec053c389ee619 Mon Sep 17 00:00:00 2001 From: Herrington Darkholme Date: Wed, 15 Feb 2017 02:40:57 +0800 Subject: [PATCH] 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 --- src/core/util/props.js | 3 ++- test/ssr/ssr-string.spec.js | 18 ++++++++++++++++++ test/unit/features/options/props.spec.js | 16 ++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/core/util/props.js b/src/core/util/props.js index 11c0245c095..a06b64b65d4 100644 --- a/src/core/util/props.js +++ b/src/core/util/props.js @@ -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 } diff --git a/test/ssr/ssr-string.spec.js b/test/ssr/ssr-string.spec.js index 9ff2d62047d..13c0a47039c 100644 --- a/test/ssr/ssr-string.spec.js +++ b/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() @@ -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) { diff --git a/test/unit/features/options/props.spec.js b/test/unit/features/options/props.spec.js index 50e5e16faf3..95f85fffef4 100644 --- a/test/unit/features/options/props.spec.js +++ b/test/unit/features/options/props.spec.js @@ -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: {