Javascript如何避免函数中的变量名冲突?
我在dropzone工作时发现了一些奇怪的事情: 这是我的滴:Javascript如何避免函数中的变量名冲突?,javascript,dropzone,Javascript,Dropzone,我在dropzone工作时发现了一些奇怪的事情: 这是我的滴: <script type="text/javascript"> var CountFiles = 0; $(document).ready(function () { Dropzone.autoDiscover = false; var new_file; const Drop1 = new Dropzone("div#myPrincipalDropzone", { u
<script type="text/javascript">
var CountFiles = 0;
$(document).ready(function () {
Dropzone.autoDiscover = false;
var new_file;
const Drop1 = new Dropzone("div#myPrincipalDropzone", {
url: "/Article/UploadFiles",
paramName: "file",
maxFiles: 1,
maxFilesize: 1200,
maxFileHeight: 840,
maxFileWidth: 840,
acceptedFiles: ".png,.jpg",
addRemoveLinks: true,
parallelUploads: 1,
renameFile: function (file) {
let newname = new Date().getTime() + '_';
console.log("Nombre en RenameFile:" + newname);
file.name = newname;
console.log("Asigno al file el nombre:" + file.name);
new_file = new File([file], newname + file.name);
console.log(new_file.name);
return new_file;
},
init: function (new_file) {
var CountFiles=0;
$(文档).ready(函数(){
Dropzone.autoDiscover=false;
新文件;
const Drop1=新的Dropzone(“div#myPrincipalDropzone”{
url:“/Article/UploadFiles”,
参数名称:“文件”,
maxFiles:1,
最大文件大小:1200,
maxFileHeight:840,
maxFileWidth:840,
acceptedFiles:“.png、.jpg”,
addRemoveLinks:是的,
并行上传:1,
重命名文件:函数(文件){
让newname=newdate().getTime()+'";
log(“Nombre en RenameFile:+newname”);
file.name=newname;
log(“Asigno al file el nombre:+file.name”);
new_file=新文件([file],newname+file.name);
console.log(新文件名);
返回新的_文件;
},
初始化:函数(新的_文件){
我注意到return语句中的变量“new_file”的值为“123847123_Filename.ext”
然而,当我尝试使用该变量调用另一个方法或函数时,我收到了新的_文件“Filename.ext”,失去了我的旧值
在google上搜索时,我发现javascript与嵌套函数之间的名称参数存在一些冲突
有办法解决这个问题吗?我需要在多个函数/方法调用中使用我的旧值。Edit 1:纠正上下文和范围之间的混淆
编辑2:引用范围链并删除“参数”范围,因为没有这样的内容
嗯,它这样做的原因是因为
一个函数可以访问两种内容:
上下文是如何起作用的:
const x = 'GLOBAL X VALUE';
function A() {
// A is referring to X within its own context
console.log(`The value of x in A is => ${this.x}`);
console.log(`The value of y in A is => ${this.y}`);
}
function B() {
// B is referring to x within the context in which it was defined
console.log(`The value of x in B is => ${x}`);
}
function C() {
// C is referring to x within the context in which it was defined
// That's why it also prints the value of the "global" context even if you
// call it within a context that has a value with the same name
console.log(`The value of x in C is => ${x}`);
}
function D(x) {
console.log(`The value of x in D is => ${x}`);
C();
}
function F() {
// A is going to have access to the context of F, but it will not have access
// to the global context
this.y = "the other value";
A.bind(this)();
console.log(`The value of x in F is => ${x}`);
}
function G(x) {
this.x = "HMMMM";
console.log(`The value of local x in G is => ${this.x}`);
console.log(`The value of param x in G is => ${x}`);
}
A(); // the value of x and y will be undefined
B(); // the value of x will be the global context
D("Parameter x value"); // the value of x will be the one passed as a parameter
F();
G("Parameter x value"); // the parameter and the local context are two different things
this
*没有两个可用的新值
,只是将其设置为本地值?为什么还要这样做?不要定义比实际需要更大范围的变量。永远不要使用var
(默认情况下,任何最新的linter都应该执行此操作)。避免在其他函数中定义复杂的函数(这一次可能会引起争议,但它往往会导致此类范围泄漏问题)。我知道,但这一次是必需的,我需要使用函数“rename file”将每个文件重命名为“unique_id”使用datetimes。稍后,我需要使用该参数使用Dropozone方法验证文件,如大小、删除等。@DanKanze-这条推理路线同样适用于goto
。一个没有犯错误的程序员可以同样有效地使用var
和let
,但var
scoping mistakes是一个非常重要的变量。让
和常量
是块范围的,而不是函数范围的。如果您想将变量移动到块的顶部,很好,但是在块范围之外定义它们也会导致许多潜在错误。但是,这些是指导原则。在某些情况下,您总是需要偏离“最佳实践”。@DanKanze“如果程序运行,它将运行“1.没有”参数上下文"您似乎引用了this
关键字2。一个函数可以访问三个以上的作用域。或者更少,这取决于它的定义位置。在到达全局作用域之前,它将是本地作用域、父作用域和其他每一个父作用域。但是只有一个上下文-this
。1.如果运行它,G将打印两个不同的val因为它有一个x
,这是一个参数,并且是在它的上下文中定义的。2.你是对的,我想我可能是歪曲了javascript在定义函数时所做的一些非常重要的绑定机制。我们需要更多的示例来演示它如何处理在中定义的=>函数G中使用x的那一面和这个.x一直是不同的。一个是参数,属于范围,另一个是属性,属于上下文。以任何方式将两者等同都是误导性的-它们之间除了名称之外没有任何共同之处,甚至这是一个意外。机制是复杂的y不一样。我现在明白你的意思了,我指的是范围和上下文,就好像它们是同一件事一样,我会尝试编辑我的答案来纠正这一点。为了进一步混淆事情……有些情况下,这个
是对窗口
的引用,可以用var
变量填充,这些变量在g中定义因此,从技术上讲,有一个var x
,以后可以作为this.x
访问。这是避免var
的另一个原因。