Vue:确定观察到的对象类型(区分数组和对象)

时间:2018-08-25 17:53:06

标签: vue.js

我有一个简单的Vue实例:

<html>
  <body>        
    <div id="container">
      <input type="text" id="container" placeholder="enter text" v-model="value">
      <p>{{ value }}</p>
    </div>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/0.11.10/vue.min.js"></script>
    <script>
      new Vue({
        el: '#container',
        data: {
          value: '',
          list: []
        },
        created: function() {
          console.log(typeof this.list); // i would like to determine type of underlaying object
        }
      });
    </script>
  </body>
</html>

https://codepen.io/anon/pen/KxVQEw?editors=1111

如何确定数据中观察到的属性的类型,比如说“列表”位于“创建的”生命周期挂钩中?

1 个答案:

答案 0 :(得分:0)

typeof []将返回"object",与typeof {}相同。如果您想知道它是JSON对象还是数组,可以使用varname.constructor.name

console.log(typeof []) // object
console.log([].constructor.name) // Array

console.log(typeof {}) // object
console.log({}.constructor.name) // Object

在您的情况下:

console.log(this.list.constructor.name) // Array
相关问题