List Object的内置属性

时间:2023-01-15 20:46:23

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

有没有办法循环Javascript对象的内置属性?

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

for ... in让我接近我想去的地方,但是“A for ... in循环不会迭代内置属性。”

5 个解决方案

#1


17  

I realize this question is three years old, but now, with ES5, it is possible:

我意识到这个问题已经有三年了,但是现在,有了ES5,它有可能:

>>> Object.getOwnPropertyNames(Object)

["prototype", "getPrototypeOf", "getOwnPropertyDescriptor", "keys", "defineProperty", "defineProperties", "create", "getOwnPropertyNames", "isExtensible", "preventExtensions", "freeze", "isFrozen", "seal", "isSealed", "length", "arity", "name", "arguments", "caller"]

[ “原型”, “getPrototypeOf”, “getOwnPropertyDescriptor”, “键”, “defineProperty”, “的DefineProperties”, “创建”, “getOwnPropertyNames”, “isExtensible”, “preventExtensions”, “冷冻”, “的isFrozen”,“ seal“,”isSealed“,”length“,”arity“,”name“,”arguments“,”caller“]

#2


5  

The answer is no. You can't enumerate properties that aren't enumerable. There are however at least two ways around this.

答案是不。您不能枚举不可枚举的属性。然而,至少有两种方法。

The first is to generate all possible combination of characters to use as test property names (think: a, b, c, ... aa, ab, ac, ad, ... ). Given that the standards community is famous for coming up with really long method names (getElementsByTagNames, propertyIsEnumerable) this method will require some patience. :-)

第一种是生成所有可能的字符组合以用作测试属性名称(想想:a,b,c,... aa,ab,ac,ad,...)。鉴于标准社区以提出非常长的方法名称(getElementsByTagNames,propertyIsEnumerable)而闻名,这种方法需要一些耐心。 :-)

Another approach is to test for known native properties from some predefined list.

另一种方法是从某些预定义列表中测试已知的本机属性。

For example: For an array you would test for all known native properties of Function.prototype:

例如:对于数组,您将测试Function.prototype的所有已知本机属性:

prototype caller constructor length name apply call toSource toString valueOf toLocaleString

...and things inherited from Object.prototype:

...以及从Object.prototype继承的东西:

__defineGetter__ __defineSetter__ hasOwnProperty isPrototypeOf __lookupGetter__
__lookupSetter__ __noSuchMethod__ propertyIsEnumerable unwatch watch

...and things inherited from Array:

......以及从Array继承的东西:

index input pop push reverse shift sort splice unshift concat join slice indexOf lastIndexOf 
filter forEach every map some reduce reduceRight

..and finally, and optionally, every enumerable property of the object you are testing:

..最后,以及可选的,您正在测试的对象的每个可枚举属性:

for (var property in myArrayObject) myPossibleProperties.push( property );

You'll then be able to test every one of those to see if they are present on the object instance.

然后,您将能够测试其中的每一个,以查看它们是否存在于对象实例上。

This won't reveal unknown non-enumerable members (undocumented, or set by other scripts), but will allow you to list what native properties are available.

这不会显示未知的非可枚举成员(未记录,或由其他脚本设置),但将允许您列出可用的本机属性。

I found the information on native Array properties on Mozilla Developer Center and MSDN.

我在Mozilla开发人员中心和MSDN上找到了有关本机Array属性的信息。

#3


0  

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

当你说“内置属性”时,你正在谈论哪一组属性?

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

来自Douglas Crockford的'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:

for in语句可以遍历对象中的所有属性名称。枚举将包括您可能不感兴趣的所有属性(包括函数和原型属性) - 因此有必要过滤掉您不想要的值。最常见的过滤器是hasOwnProperty方法,并使用typeof来排除函数:

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

#4


-1  

This will work with JSON. It hasn't been tested much:

这将适用于JSON。它没有经过多少测试:

<style>
.tree {
    margin-left:5px;
}
</style>
<div id='out'></div>
<script type="text/javascript">
data = {"feep":{"bar":{"baz":"37628","quux":{"a":"179","b":"7"}},"foo":"1025"},"Bleh":"1234"}
$('out').innerHTML = renderJSON(data)

function renderJSON(obj) {
    var keys = []
    var retValue = ""
    for (var key in obj) {
       //$('out').innerHTML = $('out').innerHTML +"<br />" + key + ", " + obj[key]      
        if(typeof obj[key] == 'object') {
            retValue += "<div class='tree'>" + key                      
            retValue += renderJSON(obj[key])
            retValue += "</div>"
        }
        else {
            retValue += "<div class='tree'>" + key + " = " + obj[key] + "</div>"
        }

       keys.push(key)
    }
    return retValue

}
</script>

#5


-1  

No you can't list object's built in properties. But you can refer to the implementer's reference. For example, to know all the methods and properties of Math object implemented on firefox, you'll use Firefox's Javascript Math Reference: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Math

不,你不能列出对象的内置属性。但您可以参考实施者的参考。例如,要了解在Firefox上实现的Math对象的所有方法和属性,您将使用Firefox的Javascript Math Reference:https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Math

#1


17  

I realize this question is three years old, but now, with ES5, it is possible:

我意识到这个问题已经有三年了,但是现在,有了ES5,它有可能:

>>> Object.getOwnPropertyNames(Object)

["prototype", "getPrototypeOf", "getOwnPropertyDescriptor", "keys", "defineProperty", "defineProperties", "create", "getOwnPropertyNames", "isExtensible", "preventExtensions", "freeze", "isFrozen", "seal", "isSealed", "length", "arity", "name", "arguments", "caller"]

[ “原型”, “getPrototypeOf”, “getOwnPropertyDescriptor”, “键”, “defineProperty”, “的DefineProperties”, “创建”, “getOwnPropertyNames”, “isExtensible”, “preventExtensions”, “冷冻”, “的isFrozen”,“ seal“,”isSealed“,”length“,”arity“,”name“,”arguments“,”caller“]

#2


5  

The answer is no. You can't enumerate properties that aren't enumerable. There are however at least two ways around this.

答案是不。您不能枚举不可枚举的属性。然而,至少有两种方法。

The first is to generate all possible combination of characters to use as test property names (think: a, b, c, ... aa, ab, ac, ad, ... ). Given that the standards community is famous for coming up with really long method names (getElementsByTagNames, propertyIsEnumerable) this method will require some patience. :-)

第一种是生成所有可能的字符组合以用作测试属性名称(想想:a,b,c,... aa,ab,ac,ad,...)。鉴于标准社区以提出非常长的方法名称(getElementsByTagNames,propertyIsEnumerable)而闻名,这种方法需要一些耐心。 :-)

Another approach is to test for known native properties from some predefined list.

另一种方法是从某些预定义列表中测试已知的本机属性。

For example: For an array you would test for all known native properties of Function.prototype:

例如:对于数组,您将测试Function.prototype的所有已知本机属性:

prototype caller constructor length name apply call toSource toString valueOf toLocaleString

...and things inherited from Object.prototype:

...以及从Object.prototype继承的东西:

__defineGetter__ __defineSetter__ hasOwnProperty isPrototypeOf __lookupGetter__
__lookupSetter__ __noSuchMethod__ propertyIsEnumerable unwatch watch

...and things inherited from Array:

......以及从Array继承的东西:

index input pop push reverse shift sort splice unshift concat join slice indexOf lastIndexOf 
filter forEach every map some reduce reduceRight

..and finally, and optionally, every enumerable property of the object you are testing:

..最后,以及可选的,您正在测试的对象的每个可枚举属性:

for (var property in myArrayObject) myPossibleProperties.push( property );

You'll then be able to test every one of those to see if they are present on the object instance.

然后,您将能够测试其中的每一个,以查看它们是否存在于对象实例上。

This won't reveal unknown non-enumerable members (undocumented, or set by other scripts), but will allow you to list what native properties are available.

这不会显示未知的非可枚举成员(未记录,或由其他脚本设置),但将允许您列出可用的本机属性。

I found the information on native Array properties on Mozilla Developer Center and MSDN.

我在Mozilla开发人员中心和MSDN上找到了有关本机Array属性的信息。

#3


0  

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

当你说“内置属性”时,你正在谈论哪一组属性?

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

来自Douglas Crockford的'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:

for in语句可以遍历对象中的所有属性名称。枚举将包括您可能不感兴趣的所有属性(包括函数和原型属性) - 因此有必要过滤掉您不想要的值。最常见的过滤器是hasOwnProperty方法,并使用typeof来排除函数:

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

#4


-1  

This will work with JSON. It hasn't been tested much:

这将适用于JSON。它没有经过多少测试:

<style>
.tree {
    margin-left:5px;
}
</style>
<div id='out'></div>
<script type="text/javascript">
data = {"feep":{"bar":{"baz":"37628","quux":{"a":"179","b":"7"}},"foo":"1025"},"Bleh":"1234"}
$('out').innerHTML = renderJSON(data)

function renderJSON(obj) {
    var keys = []
    var retValue = ""
    for (var key in obj) {
       //$('out').innerHTML = $('out').innerHTML +"<br />" + key + ", " + obj[key]      
        if(typeof obj[key] == 'object') {
            retValue += "<div class='tree'>" + key                      
            retValue += renderJSON(obj[key])
            retValue += "</div>"
        }
        else {
            retValue += "<div class='tree'>" + key + " = " + obj[key] + "</div>"
        }

       keys.push(key)
    }
    return retValue

}
</script>

#5


-1  

No you can't list object's built in properties. But you can refer to the implementer's reference. For example, to know all the methods and properties of Math object implemented on firefox, you'll use Firefox's Javascript Math Reference: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Math

不,你不能列出对象的内置属性。但您可以参考实施者的参考。例如,要了解在Firefox上实现的Math对象的所有方法和属性,您将使用Firefox的Javascript Math Reference:https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Math