JavaScript using toString on a Function object to read text content

不打扰是莪最后的温柔 提交于 2019-12-12 10:55:52

问题


Calling toString() on the function below returns different strings across browsers. I understand this is because ECMA-262 15.3.4.2 leaves wiggle room for each vendor.

Chrome returns the comments in addition to all syntax. Sadly Firefox 3.6 omits the comments. Based on Firefox's behavior I haven't tested IE, Opera, or Safari.

function foo() {
    /* comment */
    var bar = true;
}

Specifically, I am attempting to embed meta data within a specially formatted comment block within a function. Later the return value of the functions toString() method would be parsed and values returned as an object.

I've been unable to locate compatibility tables or alternatives to toString(). Does the community have any ideas? Btw, pre-processing JS files isn't an option. :(

Thanks a lot. :)


回答1:


Although not as direct as embedding comments, since functions are first-class objects in JS, you can specify arbitrary properties for them. E.g.:

> function f(x) { return x * x; }
undefined
> f
function f(x) { return x * x; }
> f.comment = 'Hello'
Hello
> f.comment
Hello

This way you can embed metadata into function objects cleanly and portably.




回答2:


I was trying to answer this question recently, and came across this. To provide an update, I was still unable to find a compatibility table, so I created the following fiddle:

https://jsfiddle.net/deamvp7r/

if((function(){ /*test*/ }).toString().match('test').length === 0) {
    alert ("NO SUPPORT");   
} else {
     alert ("SUPPORTED");   
}

and tested it manually on BrowserStack. For what it's worth, here are the browsers I've tested and what I've found so far as far as comment support in Function.toString():

  • IE8 - 11 : Supports comments
  • Firefox <= 16 : NO SUPPORT
  • Firefox >= 17 : Supports comments
  • Chrome 14 + : Supports comments
  • IOS iPhone 3GS browser + : Supports comments
  • iPad 1 + : Supports Comments
  • Android 2.3 (Galaxy) + : Supports Comments

This list is clearly far from exhaustive, so be careful if you have to support older/more obscure browsers yet. But the percentage of popular browsers that support comments currently seems pretty high.




回答3:


The "annotations" feature is a good solution for what you seem to be trying to do. Currently, it is implemented in Traceur (see here). Annotations use @ syntax, as follows:

@Anno('foo')
function func() {}

Essentially this is equivalent to

function func() {}
func.annotate = [ new Anno('foo') ];

Annotations can also be put on ES6 classes.

Even if you can't or don't want to use the new annotation syntax, adorning functions with properties to contain their metadata seems like a better idea than embedding such metadata in the function body, whether as comments or otherwise (one idea is something analogous to "use strict"), and then trying to extract it using string manipulation.

The annotation feature is still in the discussion phase, and there are parallel proposals for variations called "decorators" etc. As you probably know, the concept itself is not new, and is found in other languages such as Python, Java, and C#. Annotations are being considered for inclusion in language flavors including AtScript, TypeScript, and ES7.

See also JavaScript annotations. For a a parsing-based approach, see http://ariya.ofilabs.com/2014/04/tracking-javascript-annotations.html.



来源:https://stackoverflow.com/questions/2650593/javascript-using-tostring-on-a-function-object-to-read-text-content

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!