0

I would like to create a "name" Object, along the lines of:

let p = {
    first:              undefined,
    last:               undefined,
    get fullname()      {return `${this.first} ${this.last}`},
    set fullname(value) {throw Error("fullname can only be used as a getter.")}
}
p.first = "Tom";
p.last = "Jones";
console.log(p.fullname);
// p.fullname = "Billy Bob"; // will error

I want to have first as a property of the object, but have it not-set by default. Is doing first: undefined the correct way to do this, or how would I basically set up a 'person object'? Additionally, would it be more correct to have it as first: undefined or first: null on initialization?

David542
  • 104,438
  • 178
  • 489
  • 842
  • [What is the difference between null and undefined in JavaScript?](https://stackoverflow.com/q/5076944) | [When is null or undefined used in JavaScript?](https://stackoverflow.com/q/6429225) | [What reason is there to use null instead of undefined in JavaScript?](https://stackoverflow.com/q/6604749) – VLAZ Oct 14 '21 at 21:46
  • 2
    The only real practical difference between `null` and `undefined` is in how they interact with default function parameters. Other than that they are usually treated the same (e.g. `foo == null` is the same as `foo === null || foo === undefined`). If you want to treat them as "set" but not yet initialized I'd lean towards `null`. Choose what makes more sense to you, there is no right or wrong. – Felix Kling Oct 14 '21 at 21:46
  • Classes are much cleaner way to achieve that behavior. Though you can use object with undefined values just like you do now. – Roman Kurbatov Oct 14 '21 at 21:47
  • i am a little surprised, you are assigning a get and a set on a property which does not exist in the object ...? – Mister Jojo Oct 14 '21 at 21:48
  • @MisterJojo there's no `set` (it just raises an error, I kept that as a note to myself). Is the above way not a proper way to pass back a property that is derived from other values? – David542 Oct 14 '21 at 21:49
  • @VLAZ thanks for those links. So in the above use case, would use `undefined` or `null`? Even Felix mentions there's no right or wrong here. – David542 Oct 14 '21 at 21:51
  • 1
    @MisterJojo you shouldn't be defining a getter/setter for something that does exist on an object, though. `{x: 1, get x() { return 2;}}` makes an object with a getter only, `{get x() { return 2;}, x: 1}` creates an object without a getter, just a plain data property. – VLAZ Oct 14 '21 at 21:53
  • 1
    @David542 there is no right or wrong answer. I suppose traditionally `null` suggests something is *explicitly* set to no value, while `undefined` means it hasn't been assigned a value yet. However, don't expect all code to keep to this. Moreover, when initially creating a property/variable you can freely use either as it's basically the same - either you initialise to explicitly a missing value or just note that the value has not been set yet. It's the same and a matter of personal choice which makes more sense. – VLAZ Oct 14 '21 at 21:56
  • @VLAZ got it, thanks for the explanation. – David542 Oct 14 '21 at 22:01

0 Answers0