简体   繁体   English

如何在JavaScript中提供局部变量,全局范围

[英]How to give a local variable, a global scope in javascript

Below code snippet is Drift chat method to get the email id provided by user in chat. 下面的代码段是Drift chat方法,用于获取用户在聊天中提供的email id

I am trying to access email id (e.data.email) outside of given function scope by storing it in a global variable data_email . 我试图通过将给定功能范围之外的email id (e.data.email)存储在全局变量data_email来访问它。

I have tried three method to make data_email global variable - window , let , const . 我曾尝试three方法使data_email全局变量- windowletconst

window.drift.on("emailCapture", function(e) {
    console.log("user identified as: " + e.data.email);

    window.data_email = e.data.email;
    // let data_email = e.data.email;
    // const data_email = e.data.email;

    ga('send', 'event', {
        eventCategory: 'driftemail',
        eventAction: 'driftemailCaptured',
    });
});

console.log(data_email);

After trying all that I am getting error - Uncaught ReferenceError: data_email is not defined . 在尝试所有我得到的错误之后- Uncaught ReferenceError: data_email is not defined Please anyone suggest me a work around, i will be very thankful. 请任何人建议我变通,我将非常感激。 My goal is to access captured email outside of the given function. 我的目标是在给定功能之外访问捕获的电子邮件。

Problem you have there is the fact you are listening to an event. 您遇到的问题是您正在收听事件。 That can happen now, later or never. 这种情况现在可能会发生,以后甚至永远不会发生。 However you immediately try to console log it. 但是,您立即尝试控制台记录它。

Given the small amount of information you provided, there could be various solutions to your problem. 鉴于您提供的信息量很少,因此可能有各种解决方案。 But one solution is to make: 但是一种解决方案是:

  • outside function, that function accepts email and sets it as global 外部函数,该函数接受电子邮件并将其设置为全局

  • call that function with email as argument 以电子邮件为参数调用该函数

Something like this 像这样

window.drift.on("emailCapture", function(e) {
    console.log("user identified as: " + e.data.email);

    setChatEmail(e.data.email);

    //window.data_email = e.data.email;
    //let data_email = e.data.email;
    //const data_email = e.data.email;

    ga('send', 'event', {
        eventCategory: 'driftemail',
        eventAction: 'driftemailCaptured',
    });
});


function setChatEmail(email) {
  window.data_email = email;
}

Further reading: 进一步阅读:

How to set global variable. 如何设置全局变量。

Why are global variables bad. 为什么全局变量不好。

Your console.log runs before "emailCapture" event is executed. 您的console.log在执行“ emailCapture”事件之前运行。 Thats why its giving you undefined, try everything what you want to do after executing this event. 这就是为什么它给您未定义的原因,请在执行此事件后尝试尝试执行所有操作。

 var data_email;
 var getEmail = function() {
     console.log(data_email);
 };
 window.drift.on("emailCapture", function(e) {
    console.log("user identified as: " + e.data.email);

    window.data_email = e.data.email;
    data_email = e.data.email;
    // const data_email = e.data.email;

    ga('send', 'event', {
        eventCategory: 'driftemail',
        eventAction: 'driftemailCaptured',
    });
    getEmail();
});

You need to initialize window.data_email on top first then mutate it. 您需要首先在顶部初始化window.data_email ,然后对其进行变异。 Because the console.log will run before it's initialized. 因为console.log将在初始化之前运行。

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

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