[英]JavaScript Variable Undefined, but it is not
So I have the following JavaScript which pulls user data from Facebook: 所以我有以下JavaScript,可从Facebook提取用户数据:
As you can see in that snippet I have included the line 'console.log(full_name);', which when ran successfully outputs the users full name into the browser console. 如您在该代码段中所见,我添加了“ console.log(full_name);”行,该行在成功运行时会将用户的全名输出到浏览器控制台。
Then when I try this: 然后,当我尝试这个:
<script>document.write('Hello, ' + full_name);</script>
It doesn't work. 没用 It comes back with
Uncaught ReferenceError: full_name is not defined
. 它返回并带有
Uncaught ReferenceError: full_name is not defined
。
I am confused as it is clearly defined & works when using console.log
. 我很困惑,因为它在使用
console.log
时已明确定义并可以工作。 Someone help? 有人帮忙吗?
<script>document.write('Hello, ' + full_name);</script>
您没有定义全局变量full_name。
It definitely would throw an error. 它肯定会引发错误。
Assuming that you are including the line 假设您包括该行
<script>document.write('Hello, ' + full_name);</script>
in the HTML directly, it will be executed much before the call getLoginStatus of fb API completes, which is where you have defined the global variable full_name 直接在HTML中,它将在fb API的getLoginStatus调用完成之前执行很多,这是您定义全局变量full_name的地方
Not to mention this is a bad practice (Declaring global variables), but it is. 更不用说这是一个不好的做法(声明全局变量),但是确实如此。
In your example, invoking console.log(full_name)
works because it's defined within that code. 在您的示例中,调用
console.log(full_name)
有效的,因为它是在该代码中定义的。 Referencing full_name
in the inline script you specified won't work as it can't access the scope of the callback you pass to getLoginStatus
; 在指定的内联脚本中引用
full_name
无效,因为它无法访问传递给getLoginStatus
的回调的范围; more specifically, in your example, it will look for full_name
on the global object, which is window
in the case of the browser. 更具体地说,在您的示例中,它将在全局对象上查找
full_name
,对于浏览器而言,该对象为window
。 Thus, window.full_name
is correctly undefined
. 因此,
window.full_name
是正确undefined
。 In any case, there's also no guarantee that your callback will have been invoked by the time you invoke document.write
. 无论如何,也不能保证在调用
document.write
时将已调用回调。
To remedy this, you'll need to call document.write
within your callback or store the value elsewhere for later retrieval. 为了解决这个问题,您需要在回调中调用
document.write
或将值存储在其他位置以供以后检索。 As an aside, you should avoid document.write
. 顺便说一句,您应该避免
document.write
。
Smashing Magazine has an article on JavaScript scope. Smashing Magazine上有一篇关于JavaScript作用域的文章 。
It seems you are trying to render the name on the page. 看来您正在尝试在页面上呈现名称。 However, as @Andreas pointed out the document.write is executed as soon as that block is hit, which is before your Facebook API call is returned.
但是,正如@Andreas指出的那样,一旦击中该块,即在返回您的Facebook API调用之前,将立即执行document.write。 Instead, try placing an id on the element you want the name to be rendered in and use getElementById within your callback.
相反,尝试在要呈现名称的元素上放置一个id,并在回调中使用getElementById。 Something like this.
这样的事情。
FB.api('/me', function(response) {
document.getElementById('name').innerHTML = response.name;
});
Also, as others have mentioned, you are polluting your global scope with many variables by placing them within your call back without the var keyword. 另外,正如其他人提到的那样,您将许多变量置于全局范围内,将它们放在没有var关键字的回调中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.