gpt4 book ai didi

typescript :object.hasOwnProperty() 在继承属性上显示为真。为什么?

转载 作者:行者123 更新时间:2023-12-04 00:56:00 24 4
gpt4 key购买 nike

如果我理解正确,object.hasOwnProperty() 应该在父类的继承属性上返回 false。但是,以下代码对自己的和继承的属性都返回 true。

是我的理解/代码不正确还是 hasOwnPropery() 不正确?如果是我,我该如何区分自己的属性(property)和继承的属性(property)?

编辑:我已将我的用例添加到示例代码中。

我希望 child 的 fromDb() 只会处理它自己的属性,相反,它会覆盖 parent 的 fromDb() 设置的属性。

class Parent {
parentProp = '';
fromDb(row: {}) {
for (const key of Object.keys(row)) {
if (this.hasOwnProperty(key)) {
if (key === 'parentProp') {
// Do some required data cleansing
this[key] = row[key].toUpperCase()
} else {
this[key] = row[key];
}
}
};
return this;
}
}

class Child extends Parent {
childProp = '';
fromDb(row: {}) {
super.fromDb(row);
for (const key of Object.keys(row)) {
if (this.hasOwnProperty(key)) {
this[key] = row[key];
}
};
return this;
}
}

let row = {
parentProp: 'parent',
childProp: 'child',
}

let childObj = new Child().fromDb(row);
console.log(childObj);

控制台:

Child:
childProp: "child"
parentProp: "parent"

最佳答案

在生成的 extends 代码中,属性被复制到子类中,如下所示:

function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };

这意味着您的子类 (d) 被赋予了它自己的属性。

这实际上与使用普通 JavaScript 继承没有什么不同:

function Parent() {
this.parentProp = `I'm defined by Parent`;
}

function Child() {
Parent.call(this);
this.childProp = `I'm defined by Child`;
}

let childObj = new Child();

for (const key of Object.keys(childObj)) {
console.log(key, childObj.hasOwnProperty(key));
}

如果您就您需要实现的目标向我们提供一些指导,我相信我们会找到适合您的机制来克服这一障碍。

具体用例

对于您的特定用例,您可以通过调用父类(super class)的位置为谁“获胜”设置先例。

获取输出

childProp: "child"
parentProp: "PARENT"

让父级运行“第二”,而不是“第一”:

class Child extends Parent {
childProp = '';
fromDb(row: {}) {
for (const key of Object.keys(row)) {
if (this.hasOwnProperty(key)) {
this[key] = row[key];
}
};

super.fromDb(row); // <-- last update wins
return this;
}
}

super 动态属性内容

这将动态地从父键中排除子键和子键中的父键...添加 console.log 语句以查看内部结构...

class Parent {
parentProp = '';
fromDb(row: {}) {

const ownKeys = Object.keys(new Parent());

for (const key of Object.keys(row)) {
if (ownKeys.indexOf(key) > -1) {
if (key === 'parentProp') {
// Do some required data cleansing
this[key] = row[key].toUpperCase()
} else {
this[key] = row[key];
}
}
};
return this;
}
}

class Child extends Parent {
childProp = '';
fromDb(row: {}) {
super.fromDb(row);

const ownKeys = this.getKeys();

for (const key of Object.keys(row)) {
if (ownKeys.indexOf(key) > -1) {
this[key] = row[key];
}
};
return this;
}

getKeys() {
const childKeys = Object.keys(this);
const parentKeys = Object.keys(new Parent());

return childKeys.filter( function( el ) {
return parentKeys.indexOf( el ) < 0;
});
}
}

let row = {
parentProp: 'parent',
childProp: 'child',
}

let childObj = new Child().fromDb(row);
console.log(childObj);

关于 typescript :object.hasOwnProperty() 在继承属性上显示为真。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46825500/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com