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

javascript - ES5: Object.keys native method in JS - Stack Overflow

programmeradmin5浏览0评论

I am using Object.keys to iterate through an object's keys in JS like so:

               var cv = view.childViews;

                Object.keys(cv).forEach(function(key) {
                    this.destroyView(cv[key]);
                }.bind(this));

however, when cv is undefined or is not an object, Object.keys throws an error. Instead of putting an if statement around Object.keys to check if the typeof cv is an object, is there a native method that is better than Object.keys in that it doesn't throw an error if cv is undefined?

Isn't the whole point of:

Object.keys(cv) vs cv.keys().each()

if cv is null, the first shouldn't throw an error, whereas the latter should. What the heck, thanks JS.

If you don't help me, I will being doing this, much to my own chagrin:

              if(typeof cv === 'object'){
                    Object.keys(cv).forEach(function(key) {
                        this.destroyView(cv[key]);
                    }.bind(this));
                }

turns out, in ES6, non-object arguments to Object.keys will be "coerced" into an object, see:

I am using Object.keys to iterate through an object's keys in JS like so:

               var cv = view.childViews;

                Object.keys(cv).forEach(function(key) {
                    this.destroyView(cv[key]);
                }.bind(this));

however, when cv is undefined or is not an object, Object.keys throws an error. Instead of putting an if statement around Object.keys to check if the typeof cv is an object, is there a native method that is better than Object.keys in that it doesn't throw an error if cv is undefined?

Isn't the whole point of:

Object.keys(cv) vs cv.keys().each()

if cv is null, the first shouldn't throw an error, whereas the latter should. What the heck, thanks JS.

If you don't help me, I will being doing this, much to my own chagrin:

              if(typeof cv === 'object'){
                    Object.keys(cv).forEach(function(key) {
                        this.destroyView(cv[key]);
                    }.bind(this));
                }

turns out, in ES6, non-object arguments to Object.keys will be "coerced" into an object, see: https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys

Share Improve this question edited Jul 15, 2015 at 6:45 Alexander Mills asked Jul 15, 2015 at 1:29 Alexander MillsAlexander Mills 101k166 gold badges537 silver badges918 bronze badges 4
  • 5 How about Object.keys(cv || {}).forEach( ... ) – Pointy Commented Jul 15, 2015 at 1:31
  • 1 Unfortunately, typeof is probably the only operator that allows an undefined operant. – Derek 朕會功夫 Commented Jul 15, 2015 at 1:32
  • @Pointy That won't work when cv is undeclared: Uncaught ReferenceError: cv is not defined – Derek 朕會功夫 Commented Jul 15, 2015 at 1:33
  • 1 @Derek朕會功夫 sure but then the assignment in the line above that would fail too. (In this case I'd do the || {} in the var statement anyway.) – Pointy Commented Jul 15, 2015 at 1:35
Add a ment  | 

2 Answers 2

Reset to default 4

If you're just iterating, there's good old for...in

for(var i in null){
  alert(i);
}

for(var i in undefined){
  alert(i);
}

var obj = {hello: 'world'};

for(var i in obj){
  alert(i);
  alert(obj[i]);
}

I think @Pointy has the right answer so I'll put it here:

Object.keys(cv || {}).forEach(...)

Basically, if cv is undefined then use a dummy object and do nothing.

One other tactic would be just to do the same in the var:

var cv = view.childViews || {};
发布评论

评论列表(0)

  1. 暂无评论