簡體   English   中英

在 JavaScript 中獲取對象的所有屬性

[英]Getting all the properties of an object in JavaScript

JavaScript 有辦法獲取對象的所有屬性,包括內置屬性嗎? for... in跳過內置屬性,這通常是您想要的,但在這種情況下不是。 如果這很重要,我正在使用 Node.js,它用於調試目的,因此它不必是優雅的、快速的或可移植的。

是的,只是通過原型並獲得所有屬性

function getAllProperties(o) {
    var properties = [];
    while (o) {
        [].push.apply(properties, Object.getOwnPropertyNames(o))
        o = Object.getPrototypeOf(o);
    }
    //remove duplicate properties
    properties = properties.filter(function(value, index) {
        return properties.indexOf(value) == index;
    })
    return properties;
}

好吧,對於調試,你可以使用這個:

console.log(yourObject);

簡單快速。 在節點和瀏覽器中。 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM