gpt4 book ai didi

javascript - 关闭编译 ADVANCED_OPTIMIZATIONS 提示使用这个

转载 作者:行者123 更新时间:2023-11-29 18:14:30 24 4
gpt4 key购买 nike

我有一个Gruntfile.js,如下所示,带有闭包编译器配置

module.exports = function(grunt) {

grunt.initConfig({

pkg: grunt.file.readJSON('package.json'),
'closure-compiler': {
jstracker: {
closurePath: '/usr/local/',
js: [
'src/js/banner.js',
'src/js/lib/json.js',
'src/js/lib/jstz.js',
'src/js/init.js',
'src/js/helpers.js',
'src/js/lib/sha1.js',
'src/js/lib/murmur.js',
'src/js/lib/base64.js',
'src/js/tracker.js',
'src/js/prayagupd.js',
'src/js/constructor.js'
],
jsOutputFile: 'dist/<%= pkg.name %>.min.js',
options: {
compilation_level: 'ADVANCED_OPTIIZATIONS',
warning_level:"DEFAULT",
language_in: 'ECMASCRIPT5_STRICT'
}
}
},

qunit: {
files: ['tests/**/*.html']
},
jshint: {
files: ['Gruntfile.js', 'src/**/*.js', 'tests/*.js'],
options: {
// options here to override JSHint defaults
globals: {
jQuery: true,
console: true,
module: true,
document: true
}
}
},
watch: {
files: ['<%= jshint.files %>'],
tasks: ['jshint', 'qunit']
}
});

grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-qunit');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-closure-compiler');

grunt.registerTask('test', ['jshint', 'closure-compiler', 'qunit']);
grunt.registerTask('default', ['jshint', 'closure-compiler', 'qunit']);

};

当我执行 grunt 生成缩小的 js 时,我在 target 文件夹中看到缩小版本,

$ ls -lh dist/
total 44K
-rw-rw-r-- 1 prayagupd prayagupd 27K Jul 6 16:34 prayagupd.min.js
-rw-rw-r-- 1 prayagupd prayagupd 514 Jul 6 16:34 prayagupd.min.js.report.txt

但是以下 4 个警告都指向使用 this,稍后在浏览器中使用时会弹出错误(Uncaught ReferenceError: JSON2 is not defined)。

src/js/lib/json.js:26: WARNING - dangerous use of the global this object
if (!this.JSON2) {
^
src/js/lib/json.js:27: WARNING - dangerous use of the global this object
this.JSON2 = {};
^
src/js/leakers.js:1995: WARNING - dangerous use of the global this object
this.setContextProperty("leaksId", leaksId);
^
src/js/leakers.js:2002: WARNING - dangerous use of the global this object
this.setContextProperty("userId", userId);
^
0 error(s), 4 warning(s)

SIMPLE_OPTIMIZATIONS 关卡工作正常。

违规源代码

//src/js/lib/json.js:26
// Create a JSON object only if one does not already exist. We create the
// methods in a closure to avoid creating global variables.
if (!this.JSON2) {
this.JSON2 = {};
}

和,

   //src/js/leakers.js

/**
* Set context properties, properties which are used with every event
*/
setContextProperty: function(name, value) {
context[name] = value;
},

/**
* Set leaks ID
*/
setLeakesId: function(leakesId) {
this.setContextProperty("leakesId", leakesId);
},

/**
* Set user ID
*/
setUserId: function(userId){
this.setContextProperty("userId", userId);
}

我需要帮助解决两件事;

1) 要求闭包忽略this,这在ADVANCED_OPTIMIZATIONS下似乎不可能

2) 找到将 this 替换为并开始工作的方法。

最佳答案

编译器基本上是在警告您正在以一种不确定的方式使用“this”(您可能不小心使用了全局 this)并且可能与高级编译不兼容。您想要验证“this”值是否正确,并通过使用 @this 注释向编译器表明这一点。

此警告记录在此处: https://developers.google.com/closure/compiler/docs/error-ref

关于javascript - 关闭编译 ADVANCED_OPTIMIZATIONS 提示使用这个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24595191/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com