简体   繁体   English

console.log(名称)中的奇怪问题

[英]Weird issue in console.log(name)

When i put console.log for any variable in browser console which are undeclared it will return Uncaught ReferenceError: variable is not defined .But when i put console.log(name) in the browser console it returns empty and undefined. 当我为未声明的浏览器控制台中的任何变量添加console.log ,它将返回Uncaught ReferenceError:未定义变量 。但是当我将console.log(name)放入浏览器控制台时,它返回空和未定义。 See the image below. 见下图。 any ideas why this happens.. 任何想法为什么会这样的..

在此输入图像描述

I tested it in Chrome and Firefox developer tools. 我在Chrome和Firefox开发人员工具中测试了它。

Note : I use clear() to clear the console 注意:我使用clear()来清除控制台

name is a global variable which is in the window object. namewindow对象中的全局变量。 So when you log, it goes and finds the global one, which's value is empty string ( "" ) in your case. 因此,当您记录时,它会查找全局值,在您的情况下,其值为空字符串( "" )。

 console.log(name); console.log(window.name); 

Anything that doesn't have window attached to that and still working in console log or browser is a global object, in this case you are printing window.name in your console. 任何没有附加窗口但仍在控制台日志或浏览器中工作的东西都是全局对象,在这种情况下,您将在控制台中打印window.name。

Try to check it this way, in your console, type the below code: 尝试以这种方式检查,在您的控制台中,键入以下代码:

window.name = 'stackoverflow';

Then try to do console.log(name) again and you will see this time you seeing 'stackoverflow'. 然后尝试再次执行console.log(name),这次你会看到'stackoverflow'。 so basically the name you are printing in your console, is the window name... 所以基本上你在控制台中打印的名称是窗口名称......

For more info about window.name, visit the link below: 有关window.name的更多信息,请访问以下链接:

https://developer.mozilla.org/en-US/docs/Web/API/Window/name https://developer.mozilla.org/en-US/docs/Web/API/Window/name

This is What is happening when you type the statement console.log(name) : 这是键入语句console.log(name)时发生的情况:

  1. You are trying to access the variable name from within the global execution context(Logging in to the console in your case). 您正尝试从全局执行上下文中访问变量name (在您的情况下登录到控制台)。
  2. Since you are calling it from within the global execution context it will check if the window object has a property that's called name, because in the browser the global scope is represented by the window object. 由于您是在全局执行上下文中调用它,因此它将检查window对象是否具有名为name的属性,因为在浏览器中,全局范围由window对象表示。
  3. since you've never declared that variable before, so typing window.name or just name should return name is not defined . 既然您之前从未声明过该变量,那么键入window.name或只是name应返回名称未定义
  4. but it's returning a blank line, that's because the window object has a set of pre-defined/native properties and name is one of them. 但它返回一个空白行,这是因为window对象有一组预定义/本机属性,名称就是其中之一。
  5. window.name has by default the value "" (empty string), so it logs an empty string to your console. window.name默认具有值“”(空字符串),因此它将空字符串记录到您​​的控制台。

Now this is what's happening when you type console.log(name100) : 现在,当您键入console.log(name100)时,就会发生这种情况:

  1. Same as before(name100 instead of name). 与之前相同(name100而不是name)。

  2. Same as before(name100 instead of name). 与之前相同(name100而不是name)。

  3. You've not declared name100 neither is it a native property of the window object, so it simply returns name100 is not defined . 您没有声明name100也不是它是窗口对象的本机属性,因此它只是返回name100 is not defined

If you wanna check properties that is the shipped with the window object you can check this link: 如果您想检查窗口对象附带的属性,可以查看以下链接:

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

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