简体   繁体   English

JavaScript中的替代注释方法

[英]Alternative Comment Method in Javascript

I'm working on a CMS that minifies all scripts automatically and need to add a disclaimer as a comment the top of the .js file. 我正在使用CMS,该CMS可自动缩小所有脚本,并且需要在.js文件顶部添加免责声明作为注释。

These work with most minifiers but don't work with whatever one they're using: 这些适用于大多数缩小器,但不适用于他们使用的任何缩小器:

/*! This is the disclaimer */
/* @license This is the disclaimer */
/* @preserve This is the disclaimer */

I'm trying to find any other alternative that won't break the script. 我正在尝试寻找其他不会破坏脚本的替代方法。 The page it's used on will also have jQuery if that adds more options. 如果该页面添加了更多选项,则使用的页面也将具有jQuery。

So far my only solution seems to turn it into a string, but I'm not sure if this okay in all browsers even though it doesn't seem to break anything in most. 到目前为止,我唯一的解决方案似乎是将其转换为字符串,但是我不确定即使在大多数浏览器中都没有什么坏处,在所有浏览器中都可以这样做。

"This is the disclaimer";

// Rest of code here

I'd rather not do the following since it makes the disclaimer a little bit less obvious and I want it to stand out: 我宁愿不要执行以下操作,因为它会使免责声明变得不太明显,并且我希望它脱颖而出:

var c="This is the disclaimer";

// Rest of code here

Suggestions / ideas? 建议/想法?

A string will be fine. 一个字符串就可以了。

What proof do I have of that assertion? 我对此主张有什么证据?

Well, there's a reason they chose this: 嗯,他们选择这个是有原因的:

"use strict";

as the activator for Strict Mode, and that is that it wouldn't interfere with browsers that don't support it. 作为严格模式的激活器,那就是它不会干扰不支持它的浏览器。

So provided your disclaimer isn't "use strict", you're fine. 因此,只要您的免责声明不是“严格使用”,就可以了。

Which tool do you use to minify? 您使用哪个工具来缩小规模?
For example, with YUI Compressor you can use a ! 例如,使用YUI Compressor,您可以使用! to keep one comment: 留下一条评论:

/*!
 * This is the DISCLAIMER
 */

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM