LoginSignup
4
2

More than 5 years have passed since last update.

Computedプロパティのgetでreturnしたものがsetの引数になるっぽい

Posted at

参考図書、本家サイトにも明示されていなかったので検証したものを書いてみます。

消費税計算のサンプル

getset.js
function initial() {
    new Vue({
        el:'#msg',
        data: {
            woTax:'0'
        },
        computed: {
            wTax: {
                get: function(){
                    return parseInt(this.woTax *1.08);
                },
                set: function (val) {
                    this.woTax = Math.ceil(val / 1.08);
                }
            }
        }
    })
}

曖昧だった点

getset.js

                get: function(){
                    return parseInt(this.woTax *1.08);
                },
                set: function (val) {
                    this.woTax = Math.ceil(val / 1.08);
                }

set関数の引数valにはgetのリターンが返って来るんだろうな・・・
ということはわかるが、念のため検証

検証

getset.js

                get: function(){
                    return parseInt(this.woTax *1.08);
                },
                set: function (test) {
                    this.woTax = Math.ceil(test / 1.08);
                }

このように変更しても動作し、IDEでも変数として認識されていたので
getでリターンしたものがsetの引数として自動的に渡って来る動きになっているっぽいです。
認識違いなどツッコミありましたらお願いいたします。

4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2