Skip to content

Commit 997bf95

Browse files
authored
Merge pull request vuejs#34 from ktsn/native-class-compat
Compatibility with native class
2 parents 6b8c975 + 7e55a95 commit 997bf95

File tree

2 files changed

+16
-20
lines changed

2 files changed

+16
-20
lines changed

src/component.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,8 @@ export function componentFactory (
6565

6666
// find super
6767
const superProto = Object.getPrototypeOf(Component.prototype)
68-
if (!(superProto instanceof Vue)) {
69-
Component.prototype = Object.create(Vue.prototype)
70-
Component.prototype.constructor = Component
71-
Object.keys(Vue).forEach(key => {
72-
Component[key] = Vue[key]
73-
})
74-
}
75-
return Component.extend(options)
68+
const Super = superProto instanceof Vue
69+
? superProto.constructor as VueClass
70+
: Vue
71+
return Super.extend(options)
7672
}

src/data.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@ import { VueClass } from './declarations'
33
import { noop } from './util'
44

55
export function collectDataFromConstructor (vm: Vue, Component: VueClass) {
6-
// Create dummy Vue instance to collect
7-
// initial class properties from the component constructor.
8-
// To prevent to print warning,
9-
// the data object should inherit Vue.prototype.
10-
const data = Object.create(vm, {
11-
_init: {
12-
get: () => noop
13-
}
14-
})
6+
// override _init to prevent to init as Vue instance
7+
Component.prototype._init = function (this: Vue) {
8+
// proxy to actual vm
9+
Object.getOwnPropertyNames(vm).forEach(key => {
10+
Object.defineProperty(this, key, {
11+
get: () => vm[key],
12+
set: value => vm[key] = value
13+
})
14+
})
15+
}
1516

16-
// call constructor with passing dummy instance
17-
// `data` object will have initial data
18-
Component.call(data)
17+
// should be acquired class property values
18+
const data = new Component()
1919

2020
// create plain data object
2121
const plainData = {}

0 commit comments

Comments
 (0)