[英]access to a variable inside the function in JavaScript
For example I have this JavaScript function: 例如,我有这个JavaScript函数:
function createCroppie() {
var crop = new Croppie(document.getElementById('js-image-editor'), {
enableExif: true,
showZoomer: true,
viewport: { width: y, height: y, type: 'square'},
boundary: { width: x, height: x}
});
}
now I need to access to the "crop" variable for using in these codes: 现在,我需要访问“ crop”变量以在以下代码中使用:
$('.fa-save').on('click', function (ev) {
crop.result ({
type: 'canvas',
size: 'viewport'
}).then(function () {
console.log('upload image complete');
});
});
Since the second block of code that I wrote here is not in the same function, how can I access to the "crop" variable in createCroppie function? 由于我在此处编写的第二段代码不在同一函数中,因此如何访问createCroppie函数中的“ crop”变量?
Assuming that you initialize Croppie before the click event, you can do the following 假设您在点击事件之前初始化了Croppie,则可以执行以下操作
var myCroppie;
function createCroppie() {
myCroppie = new Croppie(document.getElementById('js-image-editor'), {
enableExif: true,
showZoomer: true,
viewport: { width: y, height: y, type: 'square'},
boundary: { width: x, height: x}
});
}
createCroppie();
$('.fa-save').on('click', function (ev) {
myCroppie.result ({
type: 'canvas',
size: 'viewport'
}).then(function () {
console.log('upload image complete');
});
});
Function variables are scoped within the function, unless closure is used. 除非使用闭包,否则函数变量将在函数范围内。 As Senal pointed out the first solution is to use a global scope for the variable. 正如Senal指出的,第一个解决方案是对变量使用全局范围。
To rewrite using closure: 要使用闭包进行重写:
function createCroppie() {
var crop = new Croppie(document.getElementById('js-image-editor'), {
enableExif: true,
showZoomer: true,
viewport: { width: y, height: y, type: 'square'},
boundary: { width: x, height: x}
}
return crop;
// or return this;
// this being the elements of the function as an object.
);
}
crop = createCroppie();
Looking up Croppie documentation it is based on creating a closure to return a object with its variables. 查找Croppie文档,它基于创建一个闭包来返回带有变量的对象。 No need to wrap a function around it. 无需在其周围包装函数。
var myCroppie = new Croppie(document.getElementById('js-image-editor'), {
enableExif: true,
showZoomer: true,
viewport: { width: y, height: y, type: 'square'},
boundary: { width: x, height: x}
};
However, you could use closure to extend the library. 但是,您可以使用闭包来扩展库。
function createCroppie() {
var crop = new Croppie(document.getElementById('js-image-editor'), {
enableExif: true,
showZoomer: true,
viewport: { width: y, height: y, type: 'square'},
boundary: { width: x, height: x}
}
function say(message) {
console.log(message);
return this;
}
return this;
);
}
crop = createCroppie();
Now crop is a new croppie with a log function, and this, (this being the following not the elements of the object of the function), works. 现在crop是具有对数函数的新的croppie,并且此函数(下面是该函数的对象的元素,而不是其后的元素)起作用。
$('.fa-save').on('click', function (ev) {
crop.log('clicked').result ({
type: 'canvas',
size: 'viewport'
}).then(function () {
console.log('upload image complete');
});
});
Note, the say function of the closure function needs to return "this", (Javascript this), so as to include the .result function allowing crop.log.result() to exist. 请注意,闭包函数的say函数需要返回“ this”(Javascript this),以便包括.result函数以允许crop.log.result()存在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.