0

So if I have an object defined like so:

var object = {
    prop1: 'val1',
    prop2: 'val2',
    prop3: {
         subprop1: 'subval1',
         subprop2: 'subval2',
         subprop3: 'subval3'
    }
}

What I am wondering is if there is a way to set a "default" value so if I call:

object.prop3

I do not want to get:

Object {subprop1: "subval1", subprop2: "subval2", subprop3: "subval3"} 

Instead I would like:

defaultsubval

Any help would be appreciated.

11mb
  • 1,339
  • 2
  • 16
  • 33
godismyjudge95
  • 922
  • 1
  • 9
  • 15
  • What exactly are you trying to achieve? Do you want to implement a custom property getter? – p.s.w.g Jun 21 '14 at 18:13
  • @p.s.w.g I would prefer something as inline as possible... for example I would love if I could do this: var object = { prop: 'defaultval' { subprop: 'subval' } }; But I know I cannot do that... – godismyjudge95 Jun 21 '14 at 18:15
  • 1
    No this can't be done. How would it ever know when you access `object.prop3` whether or not you're stopping there for the default or you're going to actually grab a property from the object? And if you did continue with another property, should it come from the object or the default value you returned? – cookie monster Jun 21 '14 at 18:17
  • @cookiemonster Maybe look ahead and see if there is a request for a sub object? I really have no idea how, if at all, this would be written. – godismyjudge95 Jun 21 '14 at 18:19
  • @LeinardoSmtih: How would it know that the request for a property on the result of `object.prop3` is supposed to come from the default value or the sub object? I'm just explaining why this can't be done. – cookie monster Jun 21 '14 at 18:20
  • @cookiemonster As I was saying it could maybe "look ahead" and see if there is a request for the sub object. So before it parses object.prop3 it would look to see if any of the sub properties are being requested (object.prop3.subprop1, etc) – godismyjudge95 Jun 21 '14 at 18:23
  • @LeinardoSmtih - javascript just doesn't work that way. You're asking for `var x = object.prop3; console.log(x.subprop1);` to show something different than `console.log(object.prop3.subprop1)`. JS just doesn't do that. You could supply a method to the `object.prop3` object that would get a default value, but you'd have to call that method or rely on a `.toString()` implicit call to retrieve it. – jfriend00 Jun 21 '14 at 18:52

2 Answers2

0

As both cookie monster and jfriend00 above noted. This is impossible with just the object notation. Javascript just does not work that way. However, I found an alternate way of doing the same thing by Getting the name of a requested subobject

Community
  • 1
  • 1
godismyjudge95
  • 922
  • 1
  • 9
  • 15
0

Here's how it can be done using Symbol's toPrimitive:

const myTheme = {
  prop1: 'val1',
  prop2: 'val2',
  color: {
    darker: 'orange',
    lighter: 'white'
  }
}

Object.defineProperty(myTheme.color, Symbol.toPrimitive, {
  value: () => 'yellow'
});

so, we got something like:

console.log('primary color: ' + myTheme.color.darker) // returns "primary color: orange"
console.log('primary color: ' + myTheme.color) // returns "primary color: yellow"