0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Vue.js の算出プロパティを使ってみた

Posted at

初めに

前回に引き続きVue.jsで学んだことのアウトプットして投稿します。

算出プロパティ

  • 算出プロパティの性質

何度アクセスしても、関数を再び実行することなく以前計算された結果を即時に返す

  • メソッドの性質

対称的に、メソッド呼び出しは、再描画が起きると常に関数を実行

ここでは入力された文字を一文字ずらして表示するメソッドを算出プロパティで書いてみました。

  <div id="app">
    <input v-model="inputText" type="text">
    {{ nextAlphabet }}
  </div>
var app = new Vue({
  el: '#app',
  data: {
    inputText: ''
  },
  computed: {
    nextAlphabet: function() {
      var alphabet = 'abcdefghijklmnopqrstuvwxyz' +
                     'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
      var alphabetList = alphabet.split('');
      var alphabetShiftList = {};
      for (let i = 0; i < alphabetList.length; i++) {
        alphabetShiftList[alphabetList[i]] = alphabetList[(i + 1) % 52]
      }
      alphabetShiftList[' '] = ' ';
      var applyShitList = this.inputText.split('').map(x => alphabetShiftList[x]);
      return applyShitList.join().replace(/,/g, '');
    }
  }
})
  • 動作結果

image.png

参考記事

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?