javascript - Create two functions to counts object properties/methods -
object.size = function(obj){ var size = 0, key = ""; for(key in obj){ if(obj.hasownproperty(key)){ size++; } } return size; }
this first function created. mission create 2 functions, 1 counts properties , other 1 counts properties , methods. (limit counting original objec, need add functions object prototype each object create has 2 functions available automatically property.)
so how do second function? (and please take @ first 1 , see if did wrong in first function?) thank much!
number of properties , methods:
object.keys(obj).length
number of properties:
#test used underscore.js function ismethod(obj, func) { return !!(obj.func && obj.func.constructor && obj.func.call && obj.func.apply); } function num_properties(obj){ var size = 0; for(key in obj){ if (!ismethod(obj, key)){ size++; } } return size; }
Comments
Post a Comment