java get set is_Is GET or SET defined in class

I try to find out, if the current derived class has a property setter. Reason is that I loop a couple of options through the constructors of extended classes. and just want to assign those properties, that are part of this class and not the base class.

So I need a possibility to find out, if the getter or setter exist for this class or not. I remove as much code as possible to show case the issue only.

This is the base class:

class baseClass{

constructor(wOptions){

//do some stuff

if(typeof wOptions == "object"){

for(let prop in wOptions){

// if(Object.getOwnPropertyDescriptor(this, prop) != undefined){

// if(this.hasOwnProperty(prop) != undefined){ /* doesn't work either */

if(typeof this[prop] != "undefined"){ /* calls the getter, if exists. */

this[prop] = wOptions[prop];

delete wOptions[prop];

}

}

}

}

get mainProperty(){

return 42;

}

set mainProperty(newValue){

return 42;

}

}

Here is the the derived class:

class derivatedClass extends baseClass{

constructor(wOptions){

super(wOptions)

//do some other stuff

let html = document.body;

Object.defineProperty(this, "html", {value: html, writable: false});

if(typeof wOptions == "object"){

for(let prop in wOptions){

// if(Object.getOwnPropertyDescriptor(this, prop) != undefined){

// if(this.hasOwnProperty(prop) != undefined){ /* doesn't work either */

if(typeof this[prop] != "undefined"){ /* calls the getter, if exists. */

this[prop] = wOptions[prop];

delete wOptions[prop];

}

}

}

}

get otherProperty(){

return html.innerText;

}

set otherProperty(newValue){

return html.innerText = newValue;

}

}

And how to initialize the object:

let options = {otherProperty: "new Text"};

let object = new derivatedClass(options);

console.log(options);

Regarding the solutions tried already:

getOwnPropertyDescriptor always returns undefined; returns the options as assigned

hasOwnProperty always returns false ; returns the options as assigned

typeof this[prop] != "undefined" calls the getter and this can be pretty bad, because html is not defined yet. Reference Error for html.innerText

Not a solution, but for verification: Removing the if clause in the derived class, changes the body text to new Text and prints an empty object in the console.

Tested in Chrome 71.

There would be some options to avoid this:

shift all properties handled by this class to another object (pretty ugly and high possibility to forget a property in the list)

use always Object.defineProperty, because they will be executed after the super call. However, it is not that pretty than the get/set functions of the class construct.

Is there any possibility to find out if there is a setter available for otherProperty, that evaluates true in the derivatedClass but not in the baseClass?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值