Skip to content

Commit

Permalink
move props definition to component prototype when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Feb 14, 2017
1 parent 406352b commit e870e6c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 26 deletions.
15 changes: 14 additions & 1 deletion src/core/global-api/extend.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import config from '../config'
import { warn, mergeOptions } from '../util/index'
import { defineComputed } from '../instance/state'
import { defineComputed, proxy } from '../instance/state'

export function initExtend (Vue: GlobalAPI) {
/**
Expand Down Expand Up @@ -48,6 +48,12 @@ export function initExtend (Vue: GlobalAPI) {
)
Sub['super'] = Super

// For props and computed properties, we define the proxy getters on
// the Vue instances at extension time, on the extended prototype. This
// avoids Object.defineProperty calls for each instance created.
if (Sub.options.props) {
initProps(Sub)
}
if (Sub.options.computed) {
initComputed(Sub)
}
Expand Down Expand Up @@ -79,6 +85,13 @@ export function initExtend (Vue: GlobalAPI) {
}
}

function initProps (Comp) {
const props = Comp.options.props
for (const key in props) {
proxy(Comp.prototype, `_props`, key)
}
}

function initComputed (Comp) {
const computed = Comp.options.computed
for (const key in computed) {
Expand Down
49 changes: 24 additions & 25 deletions src/core/instance/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ import {
noop
} from '../util/index'

const sharedPropertyDefinition = {
enumerable: true,
configurable: true,
get: noop,
set: noop
}

export function initState (vm: Component) {
vm._watchers = []
const opts = vm.$options
Expand Down Expand Up @@ -71,7 +78,9 @@ function initProps (vm: Component, propsOptions: Object) {
} else {
defineReactive(props, key, value)
}
proxy(vm, props, key)
if (!(key in vm)) {
proxy(vm, `_props`, key)
}
}
observerState.shouldConvert = true
}
Expand Down Expand Up @@ -101,7 +110,7 @@ function initData (vm: Component) {
vm
)
} else if (!isReserved(keys[i])) {
proxy(vm, data, keys[i])
proxy(vm, `_data`, keys[i])
}
}
// observe data
Expand All @@ -128,28 +137,21 @@ function initComputed (vm: Component, computed: Object) {
}
}

const computedSharedDefinition = {
enumerable: true,
configurable: true,
get: noop,
set: noop
}

export function defineComputed (target: any, key: string, userDef: Object | Function) {
if (typeof userDef === 'function') {
computedSharedDefinition.get = createComputedGetter(key)
computedSharedDefinition.set = noop
sharedPropertyDefinition.get = createComputedGetter(key)
sharedPropertyDefinition.set = noop
} else {
computedSharedDefinition.get = userDef.get
sharedPropertyDefinition.get = userDef.get
? userDef.cache !== false
? createComputedGetter(key)
: userDef.get
: noop
computedSharedDefinition.set = userDef.set
sharedPropertyDefinition.set = userDef.set
? userDef.set
: noop
}
Object.defineProperty(target, key, computedSharedDefinition)
Object.defineProperty(target, key, sharedPropertyDefinition)
}

function createComputedGetter (key) {
Expand Down Expand Up @@ -249,15 +251,12 @@ export function stateMixin (Vue: Class<Component>) {
}
}

function proxy (vm: Component, source: Object, key: string) {
Object.defineProperty(vm, key, {
configurable: true,
enumerable: true,
get: function proxyGetter () {
return source[key]
},
set: function proxySetter (val) {
source[key] = val
}
})
export function proxy (target: Object, sourceKey: string, key: string) {
sharedPropertyDefinition.get = function proxyGetter () {
return this[sourceKey][key]
}
sharedPropertyDefinition.set = function proxySetter (val) {
this[sourceKey][key] = val
}
Object.defineProperty(target, key, sharedPropertyDefinition)
}

0 comments on commit e870e6c

Please sign in to comment.