1. 程式人生 > 程式設計 >基於JavaScript實現簡易計算器

基於JavaScript實現簡易計算器

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <script src="https://unpkg.com/vue/dist/vue.js"></script>
  </head>
  <body>
    <div id="app">
      <p>{{ message }}</p>
      <p>{{ totalPrice }}</p>
    </div>

    <script>
      new Vue({
        el: '#app',
        data: {
          message: 'Hello Vue.js!',
          books: [{
              id: 1,
              name: 'HTML/HTML5基礎',
              price: 58
            },
            {
              id: 2,
              name: '高健壯性CSS',
              price: 68
            },
            {
              id: 3,
              name: '深入學習JS',
              price: 78
            }
          ]
        },
        computed: {
          totalPrice: function() {
            let result = 0;
            for (let i = 0; i < this.books.length; i++) {
              result += this.books[i].price;
            }

            return result;
          },

          totalPrice: function() {
            let result = 0;
            for (let i in this.books) {
              result += this.books[i].price;
            }

            return result;
          },
        }
      })
    </script>
  </body>
</html>