为什么javascript假定函数是变量?
我注意到JS在代码中以某种方式将函数作为变量。这是:为什么javascript假定函数是变量?,javascript,vue.js,Javascript,Vue.js,我注意到JS在代码中以某种方式将函数作为变量。这是: <template> <div id=""> <div> <input type="text" v-model="value"> <button v-on:click='[factorial(), show=!show]'>Factorial</button> <span v-show="s
<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: {
},
computed: {
factorial(x=this.value){
if (!x){return 1}
else {return x*factorial(x-1)}
}
},
mounted() {}
}
</script>
<style lang="scss">
</style>
阶乘的
{{value}}
导出默认值{
名称:“应用程序”,
数据(){
返回{
值:0,
节目:假
}
},
方法:{
},
计算:{
阶乘(x=此.value){
如果(!x){返回1}
else{返回x*阶乘(x-1)}
}
},
挂载(){}
}
错误为ReferenceError:找不到变量:阶乘
我做错了什么?您不能将参数传递给
computed
属性,这就是方法的用途
调用函数时还需要使用this
方法:{
阶乘(x){
如果(!x){
x=该值;
}
返回x*this.factorial(x-1);
},
},
不能将阶乘()用作计算函数。您应该将其作为一种方法:
阶乘的
{{value}}
导出默认值{
名称:“应用程序”,
数据(){
返回{
值:0,
节目:假
}
},
方法:{
阶乘(x=此.value){
如果(!x){返回1}
else{return x*this.factorial(x-1)}
}
},
}
这里有一个工作沙箱供您使用:
谢谢!我要试试看