Javascript 从“this”中删除函数
我有一个带有原型函数的函数,其中包含一个函数,如下所示:Javascript 从“this”中删除函数,javascript,Javascript,我有一个带有原型函数的函数,其中包含一个函数,如下所示: function parent(){ this.v = 0; console.log(this); // {v: 0} } parent.prototype.child = function(name){ this.v++; console.log(this); // {v: 1} this.childOfChild = function(name){ this.v++
function parent(){
this.v = 0;
console.log(this); // {v: 0}
}
parent.prototype.child = function(name){
this.v++;
console.log(this); // {v: 1}
this.childOfChild = function(name){
this.v++
console.log(this); // {v: 2, childOfChild: [Function]}
}
}
那么我的问题是,如何不在console.log中显示childOfChild:[函数]
我知道这是可能的,但我不记得怎么做。您可以将其定义为不可枚举属性:
parent.prototype.child = function(name) {
this.v++;
console.log(this); // {v: 1}
Object.defineProperty(this, "childOfChild", {
enumerable: false,
writable: true,
configurable: true,
value: function(name) {
this.v++;
console.log(this); // {v: 2, childOfChild: [Function]}
}
});
};
false也是默认值,因此您也可以省略enumerable。这正是我想要的!谢谢console.log依赖于实现。定义为不可枚举可能适用于缩写形式,也可能不适用于缩写形式,但大多数devtools将提供一种检查所有属性的方法,包括不可枚举属性。@Oriol它在node.js中工作,这对我来说很重要。