Skip to content

Commit 0667117

Browse files
committed
computed property shorthand
1 parent 882c16c commit 0667117

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

src/compiler.js

+13-14
Original file line numberDiff line numberDiff line change
@@ -517,10 +517,7 @@ CompilerProto.defineProp = function (key, binding) {
517517
CompilerProto.defineExp = function (key, binding) {
518518
var getter = ExpParser.parse(key, this)
519519
if (getter) {
520-
var value = binding.isFn
521-
? getter
522-
: { $get: getter }
523-
this.markComputed(binding, value)
520+
this.markComputed(binding, getter)
524521
this.exps.push(binding)
525522
}
526523
}
@@ -531,10 +528,8 @@ CompilerProto.defineExp = function (key, binding) {
531528
CompilerProto.defineComputed = function (key, binding, value) {
532529
this.markComputed(binding, value)
533530
var def = {
534-
get: binding.value.$get
535-
}
536-
if (binding.value.$set) {
537-
def.set = binding.value.$set
531+
get: binding.value.$get,
532+
set: binding.value.$set
538533
}
539534
Object.defineProperty(this.vm, key, def)
540535
}
@@ -544,15 +539,19 @@ CompilerProto.defineComputed = function (key, binding, value) {
544539
* so its getter/setter are bound to proper context
545540
*/
546541
CompilerProto.markComputed = function (binding, value) {
547-
binding.value = value
548542
binding.isComputed = true
549543
// bind the accessors to the vm
550-
if (!binding.isFn) {
551-
binding.value = {
552-
$get: utils.bind(value.$get, this.vm)
544+
if (binding.isFn) {
545+
binding.value = value
546+
} else {
547+
if (typeof value === 'function') {
548+
value = { $get: value }
553549
}
554-
if (value.$set) {
555-
binding.value.$set = utils.bind(value.$set, this.vm)
550+
binding.value = {
551+
$get: utils.bind(value.$get, this.vm),
552+
$set: value.$set
553+
? utils.bind(value.$set, this.vm)
554+
: undefined
556555
}
557556
}
558557
// keep track for dep parsing later

test/unit/specs/misc.js

+5
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,18 @@ describe('Misc Features', function () {
5151
$set: function (v) {
5252
b = v - this.a
5353
}
54+
},
55+
getOnly: function () {
56+
return this.a + 1
5457
}
5558
}
5659
})
5760

5861
assert.strictEqual(v.test, 3)
62+
assert.strictEqual(v.getOnly, 2)
5963
v.a = 2
6064
assert.strictEqual(v.test, 4)
65+
assert.strictEqual(v.getOnly, 3)
6166
b = 3
6267
assert.strictEqual(v.test, 5)
6368
v.test = 10

0 commit comments

Comments
 (0)