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

IT *_*Man 2 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>
Run Code Online (Sandbox Code Playgroud)

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

如何确定数据中观察到的属性的类型可以说“创建”生命周期钩子内的 .list ?

dzi*_*raf 5

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
Run Code Online (Sandbox Code Playgroud)

在你的情况下:

console.log(this.list.constructor.name) // Array
Run Code Online (Sandbox Code Playgroud)