List Object's built-in Properties

前端 未结 5 2118
小蘑菇
小蘑菇 2021-02-06 11:52

Is there a way for me to loop over a Javascript Object\'s built-in properties?

for...in gets me close to where I want to go, but \"A for...in loop does not iterate over

5条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-06 12:39

    When you say "built in properties", which set of properties are you exactly talking about ?

    From Douglas Crockford's 'JavaScript - The Good Parts' :

    The for in statement can loop over all of the property names in an object. The enumeration will include all of the properties—including functions and prototype properties that you might not be interested in—so it is necessary to filter out the values you don't want. The most common filters are the hasOwnProperty method and using typeof to exclude functions:

    var name; 
    for (name in another_stooge) 
    {
        if (typeof another_stooge[name] !== 'function') {
            document.writeln(name + ': ' + another_stooge[name]);
        } 
    }
    

提交回复
热议问题