開發中方法與函式的區別
阿新 • • 發佈:2021-01-07
技術標籤:javascript
- 方法(method)
在類裡的一般稱之為方法,因為方法都是和某個例項物件掛鉤的
例子:
const obj = {
counter = 0,
msg :'hello'
}
const app = new Vue({
el:'#app',
data: obj,
methods:{
add: function(){
this.counter++
},
sub: function(){
this.counter--
}
}
})
在此程式碼塊中,add和sub都被稱為方法,因為裡面的this和例項化物件app相掛鉤。
- 函式(function)
例子:
<script>
function f(){
counter++;
}
</script>
- js中既有方法又有函式,java中只有方法的概念