Best practice for deprecating functionality in a JavaScript library
I'm writing a Rails add-on that includes Javascri开发者_如何学编程pt that has API methods intended for use by application developers, and I'm wondering what I should do when I deprecate part of that functionality as the code evolves.
If this were Ruby code, I would write a message to STDOUT the first time a deprecated method was called. so the developer will notice ASAP. Perhaps, in JavaScript, I should check for the existence of console
, and write a message to console.log()
?
I believe an answer to this topic will be somewhat opinionated.
Nevertheless I would follow the way outlined by this blogpost:
- Add a @deprecated JSDoc flag.
- Mention the version that the method was deprecated.
- Figure out a timeframe for when this method will be deleted, including which version it will take place. Otherwise, based on my experience, it stays forever
- Use comments liberally to explain the implementation for the benefit of other developers or your future self. This is extremely useful if your use-case is writing a library that others use as a dependency for their work.
- Add a console warning message that indicates that the function is deprecated.
精彩评论