Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable all non-mutation functionality after Object.freeze() #2894

Closed
butterflyhug opened this issue Jan 14, 2016 · 4 comments
Closed

Enable all non-mutation functionality after Object.freeze() #2894

butterflyhug opened this issue Jan 14, 2016 · 4 comments

Comments

@butterflyhug
Copy link
Contributor

Splitting out @kruncher's request from the discussion on #1754:

I have been using Object.freeze on moment instances and this has generally achieved what I have needed; except I just discovered that the following fails:

let now = Object.freeze(moment());
if (now.isSameOrBefore(anotherTime)) { // throws exception
}

The exception:

TypeError: Can't add property _isValid, object is not extensible
 at valid__isValid (C:\git\quick-test\node_modules\moment\moment.js:93:24)
 at Moment.moment_valid__isValid [as isValid] (C:\git\quick-test\node_modules\moment\moment.js:2195:16)
 at Moment.isSame (C:\git\quick-test\node_modules\moment\moment.js:1945:44)
 at Moment.isSameOrBefore (C:\git\quick-test\node_modules\moment\moment.js:1962:21)

Can this be fixed so that Object.freeze can be used when desired?

@SeanDunford
Copy link

const moment = require('moment')
const today = Object.freeze(moment()); 
today.toString()
   => 'Tue May 02 2017 10:04:52 GMT-0400'
today.add(1, 'day')
   => moment("2017-05-03T10:04:52.889")
today.toString()
  => 'Wed May 03 2017 10:04:52 GMT-0400'

Maybe I'm missing something but Object.freeze doesn't seem to work for me at all on latest release. I know that there's a large push for immutability in 3.x but in the meantime is there reliability in Object.freeze? /

Node version: 6.10 (~lts)
Moment version: 2.18.1

@marwahaha marwahaha added Help Wanted and removed Bug labels Apr 9, 2018
@marwahaha
Copy link
Member

@butterflyhug - I'm unable to reproduce the TypeError, even in strict mode.

@SeanDunford - this seems weird. I don't know if you can trust immutability in 2.x.

I would love help understanding why this object seems mutable, even if frozen or deepFrozen.

@SeanDunford
Copy link

@marwahaha - We've switched to Luxon now. At the time we weren't ready to use 3.x.

@marwahaha
Copy link
Member

Ok, I will close and assume Object.freeze does not work for moments in 2.x.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants