- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如果我理解正确,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;
}
}
这将动态地从父键中排除子键和子键中的父键...添加 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/
你能比较一下属性吗 我想禁用文本框“txtName”。有两种方式 使用javascript,txtName.disabled = true 使用 ASP.NET, 哪种方法更好,为什么? 最佳答案 我
Count 属性 返回一个集合或 Dictionary 对象包含的项目数。只读。 object.Count object 可以是“应用于”列表中列出的任何集合或对
CompareMode 属性 设置并返回在 Dictionary 对象中比较字符串关键字的比较模式。 object.CompareMode[ = compare] 参数
Column 属性 只读属性,返回 TextStream 文件中当前字符位置的列号。 object.Column object 通常是 TextStream 对象的名称。
AvailableSpace 属性 返回指定的驱动器或网络共享对于用户的可用空间大小。 object.AvailableSpace object 应为 Drive 
Attributes 属性 设置或返回文件或文件夹的属性。可读写或只读(与属性有关)。 object.Attributes [= newattributes] 参数 object
AtEndOfStream 属性 如果文件指针位于 TextStream 文件末,则返回 True;否则如果不为只读则返回 False。 object.A
AtEndOfLine 属性 TextStream 文件中,如果文件指针指向行末标记,就返回 True;否则如果不是只读则返回 False。 object.AtEn
RootFolder 属性 返回一个 Folder 对象,表示指定驱动器的根文件夹。只读。 object.RootFolder object 应为 Dr
Path 属性 返回指定文件、文件夹或驱动器的路径。 object.Path object 应为 File、Folder 或 Drive 对象的名称。 说明 对于驱动器,路径不包含根目录。
ParentFolder 属性 返回指定文件或文件夹的父文件夹。只读。 object.ParentFolder object 应为 File 或 Folder 对象的名称。 说明 以下代码
Name 属性 设置或返回指定的文件或文件夹的名称。可读写。 object.Name [= newname] 参数 object 必选项。应为 File 或&
Line 属性 只读属性,返回 TextStream 文件中的当前行号。 object.Line object 通常是 TextStream 对象的名称。 说明 文件刚
Key 属性 在 Dictionary 对象中设置 key。 object.Key(key) = newkey 参数 object 必选项。通常是 Dictionary 
Item 属性 设置或返回 Dictionary 对象中指定的 key 对应的 item,或返回集合中基于指定的 key 的&
IsRootFolder 属性 如果指定的文件夹是根文件夹,返回 True;否则返回 False。 object.IsRootFolder object 应为&n
IsReady 属性 如果指定的驱动器就绪,返回 True;否则返回 False。 object.IsReady object 应为 Drive&nbs
FreeSpace 属性 返回指定的驱动器或网络共享对于用户的可用空间大小。只读。 object.FreeSpace object 应为 Drive 对象的名称。
FileSystem 属性 返回指定的驱动器使用的文件系统的类型。 object.FileSystem object 应为 Drive 对象的名称。 说明 可
Files 属性 返回由指定文件夹中所有 File 对象(包括隐藏文件和系统文件)组成的 Files 集合。 object.Files object&n
我是一名优秀的程序员,十分优秀!