打字稿:object.hasOwnProperty() 在继承的 属性 上显示为真。为什么?
Typescript: object.hasOwnProperty() shows true on inherited property. Why?
如果我理解正确,object.hasOwnProperty()
在 parent class 的继承属性上应该 return false。但是,以下代码 return 对自己的属性和继承的属性均成立。
我的 understanding/code 不正确还是 hasOwnPropery()
不正确?
如果是我,我该如何区分自己的财产和继承的财产?
编辑:我已将我的用例添加到示例代码中。
我希望 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"
类 只是构造函数语法的语法糖。 class 中定义的属性始终是编译为构造函数中设置的值的实例属性:
function Parent() {
this.parentProp = "I'm defined by Parent";
}
属性 并非来自原型,它是构造函数中 this
上设置的实例 属性。只有方法在原型上共享。如果您想要共享属性,则必须声明它们 static
;但它们是 class 属性 而不是原型。
在生成的 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));
}
如果您就您需要实现的目标向我们提供一些指导,我相信我们会为您找到合适的机制来克服这一障碍。
具体用例
对于您的特定用例,您可以通过调用超类的位置来设置谁 "wins" 的先例。
获取输出
childProp: "child"
parentProp: "PARENT"
让父运行"second",而不是"first":
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;
}
}
超级动态属性东西
这将动态地从父键中排除父键和子键...添加 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);
如果我理解正确,object.hasOwnProperty()
在 parent class 的继承属性上应该 return false。但是,以下代码 return 对自己的属性和继承的属性均成立。
我的 understanding/code 不正确还是 hasOwnPropery()
不正确?
如果是我,我该如何区分自己的财产和继承的财产?
编辑:我已将我的用例添加到示例代码中。
我希望 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"
类 只是构造函数语法的语法糖。 class 中定义的属性始终是编译为构造函数中设置的值的实例属性:
function Parent() {
this.parentProp = "I'm defined by Parent";
}
属性 并非来自原型,它是构造函数中 this
上设置的实例 属性。只有方法在原型上共享。如果您想要共享属性,则必须声明它们 static
;但它们是 class 属性 而不是原型。
在生成的 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));
}
如果您就您需要实现的目标向我们提供一些指导,我相信我们会为您找到合适的机制来克服这一障碍。
具体用例
对于您的特定用例,您可以通过调用超类的位置来设置谁 "wins" 的先例。
获取输出
childProp: "child"
parentProp: "PARENT"
让父运行"second",而不是"first":
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;
}
}
超级动态属性东西
这将动态地从父键中排除父键和子键...添加 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);