最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript: object property is array, but append isn't working - Stack Overflow

programmeradmin1浏览0评论
var objs = {
   'prop': []
}
objs['prop'].append('q');

Error: TypeError: objs.prop.append is not a function

Why this code is not working ?
Why console.log(typeof(objs['prop'])); is object not array ?

var objs = {
   'prop': []
}
objs['prop'].append('q');

Error: TypeError: objs.prop.append is not a function

Why this code is not working ?
Why console.log(typeof(objs['prop'])); is object not array ?

Share Improve this question asked Sep 27, 2011 at 17:21 cetvercetver 11.8k6 gold badges40 silver badges58 bronze badges 1
  • 2 typeof is a red herring. (typeof new Array()) = "object" in Javascript. – Elliot Nelson Commented Sep 27, 2011 at 17:26
Add a comment  | 

2 Answers 2

Reset to default 13

Array.push:

var objs = {
   'prop': []
}
objs['prop'].append('q');

should be:

var objs = {
   'prop': []
}
objs['prop'].push('q');

Because there are no associative arrays in JavaScript, an associative array is actually an Object. Nothing more nothing less.

发布评论

评论列表(0)

  1. 暂无评论