[Vuejs]-Why does javascript assume that a function is a variable?

0👍

You cannot pass arguments to computed properties, that is what methods are for.

You also need to use this when calling the function.

methods: {
    factorial(x) {
        if (!x) {
            x = this.value;
        }

        return x * this.factorial(x-1);
    },
},

0👍

You mustn’t use factorial() as a computed function. You should instead make it a method:

<template>
  <div id="">
      <div>
          <input type="text" v-model="value">
          <button v-on:click='[factorial(), show=!show]'>Factorial</button>
          <span v-show="show">{{value}}</span>
      </div>
  </div>
</template>
<script>
export default {
  name: 'app',
  data () {
    return {
        value : 0,
        show: false
    }
  },
  methods: {
    factorial(x=this.value){
        if (!x){return 1}
        else {return x * this.factorial(x-1)}
    } 
  },
}
</script>

Here is a working sandbox for you:

https://codesandbox.io/s/lingering-voice-p6d9l

Leave a comment