要在控制台中检索全局字符串对象实例的文字对象形式,我们只需执行以下操作:
var myString = new String("Hello Stackoverflow!");
console.log(myString);
/* console outputs: String {0: "H", 1: "e", 2: "l",..., 18: "w",
19: "!", length: 20, [[PrimitiveValue]]: "Hello Stackoverflow!"} */
但是,当创建全局 RegExp 对象的正则表达式实例并尝试获取对象文字形式时,它将不起作用,控制台将只输出正则表达式模式和标志。
var myRegexp = new RegExp("\d+","g");
console.log(myRegexp);
/* console outputs: /d+/g while I would expect RegExp{..., global:true,...}
basically the look of an object with curly braces and properties*/
如何检索该正则表达式对象实例及其所有属性并将其显示在控制台中?
RexExp 的所有属性都不是可枚举的,因此无法以非常简单的方式显示。此外,覆盖对象的toString()
方法,您可以更改要打印的内容。例如:
var myRegexp = new RegExp("\d+","g");
myRegexp.toString = function() {
return 'I am a regex and I dont want to show my properties!';
};
console.log(myRegexp);
话虽如此,我在MDN帖子之后创建了一个jsfiddle(链接将紧随其后(,它将打印您想要的所有属性。我刚刚在 jsfiddle 和这里实现了一个示例,但您需要稍微使用它才能获得您想要的打印并具有您想要的正确属性
var SimplePropertyRetriever = {
getOwnEnumerables: function(obj) {
return this._getPropertyNames(obj, true, false, this._enumerable);
// Or could use for..in filtered with hasOwnProperty or just this: return Object.keys(obj);
},
getOwnNonenumerables: function(obj) {
return this._getPropertyNames(obj, true, false, this._notEnumerable);
},
getOwnEnumerablesAndNonenumerables: function(obj) {
return this._getPropertyNames(obj, true, false, this._enumerableAndNotEnumerable);
// Or just use: return Object.getOwnPropertyNames(obj);
},
getPrototypeEnumerables: function(obj) {
return this._getPropertyNames(obj, false, true, this._enumerable);
},
getPrototypeNonenumerables: function(obj) {
return this._getPropertyNames(obj, false, true, this._notEnumerable);
},
getPrototypeEnumerablesAndNonenumerables: function(obj) {
return this._getPropertyNames(obj, false, true, this._enumerableAndNotEnumerable);
},
getOwnAndPrototypeEnumerables: function(obj) {
return this._getPropertyNames(obj, true, true, this._enumerable);
// Or could use unfiltered for..in
},
getOwnAndPrototypeNonenumerables: function(obj) {
return this._getPropertyNames(obj, true, true, this._notEnumerable);
},
getOwnAndPrototypeEnumerablesAndNonenumerables: function(obj) {
return this._getPropertyNames(obj, true, true, this._enumerableAndNotEnumerable);
},
// Private static property checker callbacks
_enumerable: function(obj, prop) {
return obj.propertyIsEnumerable(prop);
},
_notEnumerable: function(obj, prop) {
return !obj.propertyIsEnumerable(prop);
},
_enumerableAndNotEnumerable: function(obj, prop) {
return true;
},
// Inspired by http://stackoverflow.com/a/8024294/271577
_getPropertyNames: function getAllPropertyNames(obj, iterateSelfBool, iteratePrototypeBool, includePropCb) {
var props = [];
do {
if (iterateSelfBool) {
Object.getOwnPropertyNames(obj).forEach(function(prop) {
if (props.indexOf(prop) === -1 && includePropCb(obj, prop)) {
props.push(prop);
}
});
}
if (!iteratePrototypeBool) {
break;
}
iterateSelfBool = true;
} while (obj = Object.getPrototypeOf(obj));
return props;
}
};
var myRegexp = new RegExp("\d+","g");
SimplePropertyRetriever.getPrototypeNonenumerables(myRegexp).forEach(function(el) {
console.log(el + ": " + myRegexp[el]);
});
这里是链接:
https://developer.mozilla.org/it/docs/Web/JavaScript/Enumerability_and_ownership_of_properties
这里有一个jsfiddle:
https://jsfiddle.net/t3bp4tnq/1/
我希望这有帮助
console.dir 打印对象的树表示形式。
console.dir(myRegexp);
myRegexp
的source
和global
属性都不可枚举。您可以通过对属性的特定引用来获取属性的值
var myRegexp = new RegExp("\d+","g");
var myRegexpProps = {};
var props = ["source", "flags", "global", "multiline", "sticky", "unicode"];
for (let prop of props) myRegexpProps[prop] = myRegexp[prop];
console.log(myRegexp.source, myRegexp.global, myRegexp.flags
, myRegexp.ignoreCase, myRegexp.multiline);
console.log(myRegexpProps);