首页 > 解决方案 > How to extract comments from Object literals in javascript

问题描述

I'm trying to extract comments from different javascript objects. In the case of a function, I can easily do it by using .toString and then apply a regex to the function output. However, If the object is an object literal with comments I can't extract the comments because of course toString returns [object Object] I've tried use Object.toSource but that seems to strip out the comments :/. I'm wondering if there is any smart hack to do this.

I release I could wrap the object literal in a function and then call .toString but I don't have access to the where it is defined - I am just passed the reference.

For clarity:

const myFunction = () => {
  // I can extract this
  return 'foo'
}

const myObject = {
  // I can't extract this
  foo: 'bar'
}

Is there any way to do this?

标签: javascriptnode.jsobject

解决方案


在运行时这是不可能的。您应该使用文件解析器或类似 jsDoc 的东西会更好。


推荐阅读