1

I am creating a hasOwnProperty method for my (class) object in JavaScript.

What this does is: I feed it an object like this:

{
     "key": "value",
    "anotherKey": false
}

and it should loop through this and create based on the keys in the object above properties in my (class) object. However this is not possible, because the variable I pass as property, is literally the variable, instead of the value it stores.

Example:

let x = "foo";

this.x = "bar"; //x is the property, instead of the value assigned to x.

What I have tried so far:

class foob {
    //other methods etc
    constructor(externalObj) {
        this._startValues = externalObj;
        this.hasOwnProperty({"aKeyINeedToBeCertainHasBeenSet": "a default value if the key >aKeyINeedToBeCertainHasBeenSet< has not been set"});
     }
    hasOwnProperty(dynamicObject) {
        let $this = this;
        let property;
        Object.keys(dynamicObject).forEach(function(dynamicObjectKey, index) {
            //variable property should now be: _aKeyINeedToBeCertainHasBeenSet -> 
              //how do I use the value the variable "property" stores as the property name 
              //of my object, rather then having JS use the variable "property" as property name?
            property = `_${dynamicObjectKey}`;

            if ($this._startValues.hasOwnProperty(`${dynamicObjectKey}`)) {
                //value of the variable property should be the "property name"!
                $this.property = $this._startValues[dynamicObjectKey];
            } else {
                //value of the variable property should be the "property name"!
                $this.property = dynamicObject[dynamicObjectKey]
            }
        });
    }
}

//aKeyINeedToBeCertainHasBeenSet is not set, and therefore a default value should be provided to this property.
new foob({
     "key": "value",
    "anotherKey": false
});

Is there any way to achieve this? I have to use hasOwnProperty too much right now, which is cluttering my code.

  • 3
    Do you want `$this[property] = ...` ? – Nick Parsons Jun 12 '21 at 00:43
  • How about moving a step higher and explaining exactly what it is you are trying to do here. The idea that a variable doesn't represent it's own value makes no sense. Your example of a variable named `x` and a property x have no relation to each other – charlietfl Jun 12 '21 at 00:45
  • @charlietfl editted for clarity. Maybe if JavaScript could get rid of `{}` and `class foo {}` -> new foo() both being "objects", it would be easier to follow I guess. – I try so hard but I cry harder Jun 12 '21 at 01:03
  • @NickParsons I editted the question. I have no idea who closed the question as well, but my question doesn't even come close as to what it was closed for. – I try so hard but I cry harder Jun 12 '21 at 08:44
  • @ItrysohardbutIcryharder I'm not sure I understand, how doesn't it answer your question? You are doing `$this.property = ...` which sets the literal key "property" on your object, but, as the answer suggests, you need to use `$this[property] = ...` in your if-statements to set the property. Other than that issue, your constructor method should be `constructor` and not `construct` (if you want it to run when you're creating your object). Doesn't that do what you are after? – Nick Parsons Jun 12 '21 at 08:56
  • 1
    @NickParsons ah yikes... that indeed IS the correct solution. You are right regarding the constructor. It had gotten kind of late and I must've made the typo when editting. I will edit that out again. – I try so hard but I cry harder Jun 12 '21 at 09:49

0 Answers0