# \[Solved\] Question: "get" v.s. "default"

**URL:** https://forums.bitovi.com/t/solved-question-get-v-s-default/991
**Category:** CanJS
**Created:** [November 12, 2018, 10:30pm UTC](https://forums.bitovi.com/t/solved-question-get-v-s-default/991 "2018-11-12T22:30:15Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![leoj3n](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.bitovi.com/leoj3n/32/174_2.png) [@leoj3n](https://forums.bitovi.com/u/leoj3n)
#### Post date: [November 12, 2018, 10:30pm UTC](https://forums.bitovi.com/t/solved-question-get-v-s-default/991/1 "2018-11-12T22:30:15Z")

</div>

Sometimes I see:

```auto
const AppViewModel = DefineMap.extend('App', {
  get title() {
    return 'My Cool App';
  },
});

```

And other times I see:

```auto
const AppViewModel = DefineMap.extend('App', {
  title: {
    default: 'My Cool App';
  },
});

```

What’s the difference? I don’t understand the inconsistency outside of perhaps personal preference.

EDIT:

I’ve noticed I’ll get this error in dev when returning an object from a get function:

> <https://github.com/canjs/can-define/blob/0a120d6204e46943fe44e91b6fc55697037cc2f9/can-define.js#L359>

So I’m guessing you only return strings from a get function? Not really sure where to look in the documentation to get clarification on this. Skimmed through `can-define` but nothing popped out.

---

<div class="post-metadata">

### Author: ![justinbmeyer](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.bitovi.com/justinbmeyer/32/179_2.png) [@justinbmeyer](https://forums.bitovi.com/u/justinbmeyer)
#### Post date: [November 13, 2018, 2:33am UTC](https://forums.bitovi.com/t/solved-question-get-v-s-default/991/2 "2018-11-13T02:33:56Z")

</div>

Did you happen to see:

> **[get | can-define | Observables | API Docs | CanJS](https://canjs.com/doc/can-define.types.get.html)**
>
> Specify what happens when a certain property is read on a map. get functions work like a can-compute and automatically update themselves when a dependent observable value is changed.

and

> **[default | can-define | Observables | API Docs | CanJS](https://canjs.com/doc/can-define.types.default.html)**
>
> Returns the default value for instances of the defined type. The default value is defined on demand, when the property is read for the first time.

while skimming can-define? I’d like to make sure folks can find these. You can see them here in the sidebar:

![image](https://canada1.discourse-cdn.com/flex035/uploads/donejs/original/1X/974db81e4dca95281e9984b8da160f2096eb856c.png)

`default` is probably easier to understand. It creates a default value. This can happen very fast. It also can only happen once per instance. Both of those examples should probably be using `default`. I see `get` used a lot of places where `default` is probably better. Despite this `get` probably isn’t harmful.

`get` is a computed getter property. I’d read through the docs to get a better understanding. If you have other questions after reading, let me know.

> I’ve noticed I’ll get this error in dev when returning an object from a get function:

What you return has nothing to do with that error. That error happens because a `get` property is being set and the getter has no way of accessing the value that was set.

Think of it like lighting a match within the event horizon of a black hole. It’s pointless.

For example:

```auto
var Type = DefineMap.extend({
  get foo(){
    return 32; // return object, string, anything, doesn't matter
  }
})

var instance = new Type();
instance.foo = {} // set an object, string, anything, doesn't matter

```

`foo` is ALWAYS going to return 32 so setting it to anything else is “pointless” and we give that warning.

---

<div class="post-metadata">

### Author: ![leoj3n](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.bitovi.com/leoj3n/32/174_2.png) [@leoj3n](https://forums.bitovi.com/u/leoj3n)
#### Post date: [November 13, 2018, 4:20pm UTC](https://forums.bitovi.com/t/solved-question-get-v-s-default/991/3 "2018-11-13T16:20:18Z")

</div>

I guess one problem is the syntax on the docs never covers the usage like:

```js
  get title() {
    return 'My Cool App';
  },

```

As far as I could tell, using `get` like this wasn’t mentioned on any of the pages you linked.

Maybe this is an ES6 feature (or something?) that essentially translates to:

```js
  title: {
    get: function() { return 'My Cool App'; }
  },

```

Although, I’m just making an assumption because I don’t see this usage mentioned anywhere in the `can-define` documentation.

It’s also still not clear when `default` and `get` are interchangeable and I don’t see any advice on when to use either or any cross-referencing between the two documentation pages you linked.

So, in addition to explaining the different `get` syntaxes, I think a note or short paragraph under usage that explains the different use cases pointing `default` to `get` and `get` to `default` would be helpful for novice users that might see the same thing done in two different ways without any clear reason as to why.

EDIT:

Perhaps it would be helpful to summarize the relevant parts of this MDN article about `getter`:

> **[get - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/get)**
>
> The get syntax binds an object property to a function that will be called when that property is looked up. It can also be used in classes.

> The get syntax binds an object property to a function…
> 
> - It must have exactly zero parameters

Something along the lines of this explanation:

> <https://stackoverflow.com/questions/37366181/passing-argument-to-javascript-object-getter/37366206#37366206>

EDIT:

I see this explanation in the `place-my-order` guide:

> <https://github.com/donejs/place-my-order/blob/b6336c952c8e5d9f5bfecbf0444db25437d80bdf/src/components/header.component#L30-L37>

Something like that might be useful to include in the CanJS docs. A question I have after reading that is if you specify `default`, does that stop external values from setting that property?

---

<div class="post-metadata">

### Author: ![leoj3n](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.bitovi.com/leoj3n/32/174_2.png) [@leoj3n](https://forums.bitovi.com/u/leoj3n)
#### Post date: [November 13, 2018, 4:31pm UTC](https://forums.bitovi.com/t/solved-question-get-v-s-default/991/4 "2018-11-13T16:31:59Z")

</div>

> [@justinbmeyer](#):
>
> Think of it like lighting a match within the event horizon of a black hole. It’s pointless.

I was trying to use `get` to return the value of `env` instead of using `default` like:

```js
  env: {
    default: () => ({ NODE_ENV: 'development' }),
  },

```

So, the event horizon error was happening even though I wasn’t setting `env` anywhere.

I assume `steal` or `can` is setting `env` on the view model?

This is actually something I’ve been trying to figure out for a while, where does `this.env` come from and who is setting it? Does it get set on all view models directly or is it a global variable or does it only get set on the root app view model somehow?

---

<div class="post-metadata">

### Author: ![justinbmeyer](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.bitovi.com/justinbmeyer/32/179_2.png) [@justinbmeyer](https://forums.bitovi.com/u/justinbmeyer)
#### Post date: [November 14, 2018, 9:32pm UTC](https://forums.bitovi.com/t/solved-question-get-v-s-default/991/5 "2018-11-14T21:32:11Z")

</div>

done-ssr sets it. Only gets set on the app view model. @matthewp would know more.

---

<div class="post-metadata">

### Author: ![leoj3n](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.bitovi.com/leoj3n/32/174_2.png) [@leoj3n](https://forums.bitovi.com/u/leoj3n)
#### Post date: [November 16, 2018, 1:59am UTC](https://forums.bitovi.com/t/solved-question-get-v-s-default/991/6 "2018-11-16T01:59:29Z")

</div>

I think that’s all I need to know, although I would greatly appreciate a link to the source code that does said setting of `env` on the app view model as I wasn’t able to find it (not really sure which repo to look in).

---

<div class="post-metadata">

### Author: ![leoj3n](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.bitovi.com/leoj3n/32/174_2.png) [@leoj3n](https://forums.bitovi.com/u/leoj3n)
#### Post date: [November 19, 2018, 11:52pm UTC](https://forums.bitovi.com/t/solved-question-get-v-s-default/991/7 "2018-11-19T23:52:45Z")

</div>

@justinbmeyer @matthewp Currently I’m noticing `this.env.NODE_ENV` goes from `production` to undefined after reattachment, which means my app can’t correctly check the env in the running app client stache, which I believe is supposed to be possible.

EDIT:

I’ve opened an issue in done-ssr regarding the issues I’ve been having with `env.NODE_ENV`:

[https://github.com/donejs/done-ssr/issues/610](https://github.com/donejs/done-ssr/issues/610)

Although, I might just be misunderstanding limitations in detecting “production” on the client side?
