Javascript 确定属性是来自父级还是子级
我想在子类中编写一个简单的函数,只返回它自己的键(而不是父键) 结果:Javascript 确定属性是来自父级还是子级,javascript,typescript,Javascript,Typescript,我想在子类中编写一个简单的函数,只返回它自己的键(而不是父键) 结果: 我的唯一子键是:[\u parentAttribute,childAttribute] 所需结果:我的唯一子键是:[childAttribute] 这可能吗?最终得到的结果是这样的。但是,我觉得自己有点不对劲,我愿意接受更好的答案: class Parent{ protected _parentAttribute: string; protected _parentAttribute2: string;
我的唯一子键是:[\u parentAttribute,childAttribute]
所需结果:我的唯一子键是:[childAttribute]
这可能吗?最终得到的结果是这样的。但是,我觉得自己有点不对劲,我愿意接受更好的答案:
class Parent{
protected _parentAttribute: string;
protected _parentAttribute2: string;
protected _parentKeyList: Array<string>;
constructor() {
this._parentAttribute='test';
this._parentAttribute2='another value';
this._parentKeyList=['']; //Oddly necessary...
this._parentKeyList=Object.keys(this); //Must be at the end of constructor
}
class Child extends Parent{
childAttribute: string;
constructor() {
super();
const uniqueKeys=_.difference(Object.keys(this),this._parentAttributes); //Using lodash
console.log("My unique child keys are:", uniqueKeys);
}
let child=new Child();
类父类{
受保护的\u parentAttribute:字符串;
受保护的\u parentAttribute2:字符串;
受保护的\u parentKeyList:数组;
构造函数(){
这个;
这是._parentAttribute2='另一个值';
这个。_parentKeyList=[''];//奇怪的是,它是必需的。。。
this.\u parentKeyList=Object.keys(this);//必须位于构造函数的末尾
}
类子级扩展父级{
childAttribute:字符串;
构造函数(){
超级();
const uniqueKeys=..difference(Object.keys(this),this.\u parentAttributes);//使用lodash
log(“我唯一的子键是:”,uniquekey);
}
让孩子=新孩子();
在super()
之后,子对象和父对象的属性相等
打字稿:
类父类{
_parentAttribute1:string=“测试”
_parentAttribute2=‘测试’;
构造函数(){
}
}
类子级扩展父级{
getParentPropertyNames():数组{
删除this.parentPropertyNames;
return Object.getOwnPropertyNames(this)
}
//这就是在super()之后立即被称为的魔力
parentPropertyNames:Array=this.getParentPropertyNames()
//
childAttribute1=‘测试’;
childPropertyNames!:字符串[]
构造函数(){
超级();
}
获取uniqueNames(){
this.childPropertyNames=Object.getOwnPropertyNames(this)
//@ts忽略
.filter(name=>!this.parentPropertyNames.includes(name))//浪费计算
console.log(this.childPropertyNames)
返回此.childPropertyNames
}
}
让孩子=新孩子();
独生子女
已解析为在SO上运行
类父类{
构造函数(){
这一点。_parentAttribute1=“测试”;
这是._parentAttribute2=‘test’;
}
}
类子级扩展父级{
构造函数(){
超级();
//这就是在super()之后立即被称为的魔力
this.parentPropertyNames=this.getParentPropertyNames();
//
this.childAttribute1='test';
}
getParentPropertyNames(){
删除this.parentPropertyNames;
返回Object.getOwnPropertyNames(this);
}
获取uniqueNames(){
this.childPropertyNames=Object.getOwnPropertyNames(this)
//@ts忽略
.filter(name=>!this.parentPropertyNames.includes(name));//浪费计算
console.log(this.childPropertyNames);
返回此.childPropertyNames;
}
}
让孩子=新孩子();
child.uniqueNames;
首先,在顶级类中创建一个变量,然后在该变量中存储顶级类中的键。在子类中使用筛选器函数来筛选顶级变量。我认为这种方法没有什么不好的。筛选器应该可以正常工作,并且这种方法每次都可以工作
class Parent{
protected _parentAttribute: string;
protected topKeys;
constructor() {
this._parentAttribute='test';
this.topKeys = 'test' // asign something it so it comes in your property names
let somevar = Object.getOwnPropertyNames(this) // get all the properties
this.topKeys = somevar // put them in this variable
}
}
class Child extends Parent{
public childAttribute: string;
constructor() {
super();
this.childAttribute = 'test'
let keyofChild = Object.keys(this).filter(keys => !this.topKeys.includes(keys))
console.log("My unique child keys are:", keyofChild); // childAttribute
}
}
let child = new Child();
您可以在两个类中生成函数,返回键,然后在子类中从父类中筛选子数组,所以最终只得到子道具this.keys(-)超级钥匙?只是一个guess@Estradiaz不,因为
这个.key
可能还包含parent@Estradiaz-您如何准确访问super.keys?类似于Object.keys(super)不起作用。是的,没有线索只是尝试过-超级需要一个成员访问-所以nvm Sryth是一个坏黑客。尝试代替为什么拒绝投票?我告诉过你我的答案是黑客。你能给我举个例子吗?在childs构造函数中,this.hasOwnProperty('childAttribute')
和this.hasOwnProperty('u parentAttribute'))
两者都返回true
。此外,Object.key(this)
和Object.getOwnPropertyNames(this)
两者都返回相同的内容。我知道这两个函数之间的差异,但不确定如何将其应用于此场景。因此,我为什么要问这个问题……不是我的投票,但这是一个非常糟糕且不可扩展的黑客行为。您有很多更好的选择。请尝试Object.getOwnPropertyNames()
或循环Object.keys
并检查Object.hasOwnProperty(key)
@kmoney12只是为了让您知道-不需要这个黑客-一切都可以在孩子身上完成-询问smth是否不清楚我的解决方案
class Parent {
_parentAttribute1: string = "test"
_parentAttribute2 = 'test';
constructor() {
}
}
class Child extends Parent {
getParentPropertyNames(): Array<string>{
delete this.parentPropertyNames;
return Object.getOwnPropertyNames(this)
}
// this is the magic as it get called immediatly after super()
parentPropertyNames: Array<string> = this.getParentPropertyNames()
// </magic>
childAttribute1 = 'test';
childPropertyNames!: string[]
constructor() {
super();
}
get uniqueNames(){
this.childPropertyNames = Object.getOwnPropertyNames(this)
//@ts-ignore
.filter(name => !this.parentPropertyNames.includes(name)) // wastefull computation
console.log(this.childPropertyNames)
return this.childPropertyNames
}
}
let child = new Child();
child.uniqueNames
class Parent{
protected _parentAttribute: string;
protected topKeys;
constructor() {
this._parentAttribute='test';
this.topKeys = 'test' // asign something it so it comes in your property names
let somevar = Object.getOwnPropertyNames(this) // get all the properties
this.topKeys = somevar // put them in this variable
}
}
class Child extends Parent{
public childAttribute: string;
constructor() {
super();
this.childAttribute = 'test'
let keyofChild = Object.keys(this).filter(keys => !this.topKeys.includes(keys))
console.log("My unique child keys are:", keyofChild); // childAttribute
}
}
let child = new Child();