2


I have a function, that check computed style property values of two DOM elements and it should return true or false if every property are equivalent.

checkUserBlock: function (userBlockSelector, properties) {
        var checker = this.setCheckingStyle(userBlockSelector, properties);
        var userBlockCompStyle = getComputedStyle(this.getUserBlock(userBlockSelector));
        var checkBlockCompStyle = getComputedStyle(checker);
        var checkingStyle = this.parseCheckingStyle(properties);
        for(var key in checkingStyle){
            return (userBlockCompStyle.getPropertyValue([key].toString()) == checkBlockCompStyle.getPropertyValue([key].toString()));
        }
    }

I have problem with returning result for all properties and I think about every() function, but its only for array. How i can use it for object, or you can offer a different solution without jQuery?

whois42
  • 176
  • 13

2 Answers2

2

You could define a function like this:

function every(callback) { 
  for(prop in this) { if(!callback(this[prop])) return false; };
 return true;
}

which you can slap on an object and it'll iterate over its enumerable properties:

var obj = {1: 1, b: 3, E: 4};
Object.defineProperty(obj, 'every', { value: every, enumerable: false, writable: true, configurable: true });
obj.every(function(item) { return item < 5; })
//true
obj.every(function(item) { return item < 1; })
//false

Or you can monkey patch Object.prototype to give the every method to all objects:

Object.defineProperty(Object.prototype, 'every', { value: every, enumerable: false, writable: true, configurable: true });

Alternatively you could use Object.getPropertyNames if you don't want every to apply to prototypically inherited properties:

 function every(callback) { 
      var propertyNames = Object.getOwnPropertyNames(this);
      var length = propertyNames.length;
      for (var i=0; i < length; i++) {
         if(!callback(this[propertyNames[i]])) return false; 
      }
     return true;
 }

This you can slap onto your object's protytpe without having to worry about making it in inenumerable:

var obj = Object.create({every: every}); 
obj[1]=1; obj.b=3; obj.E=4;
obj.every(function(item) { return item < 5; })
//true
obj.every(function(item) { return item < 1; })
//false
Petr Skocik
  • 58,047
  • 6
  • 95
  • 142
  • No, you cannot slap a method to `Object.prototype` without worrying about enumerability, like your last example does? – Bergi Apr 06 '15 at 12:38
  • @Bergi Thanks for the comment. I mistakenly asssumed that Object.getProrotypeOf({}) !== Object.prototype. I've fixed the last example so that it has it has an unshared prototype now. – Petr Skocik Apr 06 '15 at 13:23
0

This will give you an array of property names, which you could then iterate over (adapted from https://stackoverflow.com/a/328418/4670652):

function propertiesArray(obj) {
   var propList = [];
   for(var propName in obj) {
      if(typeof(obj[propName]) != "undefined") {
         propList.push(propName);
      }
   }
   return propList;
}
Community
  • 1
  • 1
Matt
  • 311
  • 1
  • 7