名称已在控制台中定义!? [英] name already defined in console!?

查看:86
本文介绍了名称已在控制台中定义!?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人能解释为什么这段代码会返回

Could anyone explain why this code returns



在firebug控制台中

in firebug console?

不要低估,认为这只是一个错字!:O
我很想知道为什么它不会抛出错误 !!!

function mds() {
  var {
    namex,
    score,
    className
  }
  = {
    namex: 'NAME',
    score: '10',
    className: 'Oop'
  };
  this.test = function () {
    return name;
  };
}
var x = new mds();
x.test()

我也很想听到有关此类型的更多细节变量映射(或链接)?

I would also love to hear more details on this type of variable mapping (or a link) ?

更新:

我想知道原因<在控制台中使用值预定义了strong>名称?

I would like to know why name is predefined with value "" in console?

推荐答案

JavaScript这里的部分解释很简单。

The JavaScript part of the explanation here is simple.

执行此操作时:

  this.test = function () {
    return name;
  };

首先会查找名为 name的变量在函数 this.test()的范围内。当在那里找不到 name 时,它也会检查父函数的范围。由于 name 也没有在那里定义,它将在全局范围内查看,即 window ,并返回 window.name 的值,在本例中为空字符串。当你在控制台(或Firebug)中运行 name 时,你会看到这个( window.name )。这很简单。

It will first look for a variable named name in the scope of the function this.test(). When name is not found there, it will check in the parent function's scope as well. And since name is not defined there as well, it will look in the global scope, which is window, and returns the value of window.name, which in this case is an empty string. This (window.name) is what you are seeing when you run name in the console (or Firebug). This much is simple.

window.name 的值是根据它在HTML标准中的定义决定的(不符合ECMAScript规则)。 这个 MDN页面说:

The value of window.name is decided according to it's definition in the HTML Standard (not according to ECMAScript rules). This MDN page says:


window.name - 获取/设置窗口的名称。

window.name - Gets/sets the name of the window.

在你的情况下,窗口的名称是空的,因此你得到的空字符串。这是为什么 window.name 为空的简单解释。

And in your case, the name of the window is empty, hence the empty string you are getting. That's the simpler explanation of why window.name is empty.

如果你真的想了解为什么 window.name 此处为空,您需要查看 HTML规范,了解浏览上下文的内容。它表示 window.name 的值将是当前浏览上下文的名称

If you really want to understand why window.name is empty here, you need to check out the HTML Specification for what it says about browsing contexts. It says that the value of window.name will be the name of the current browsing context.


浏览上下文是将Document对象呈现给用户的环境。 (来自规范

A browsing context is an environment in which Document objects are presented to the user. (from the spec)

例如,当您使用以下代码在HTML中创建链接时, target 属性指定目标浏览要打开页面的上下文。

For example, when you create a link in HTML using the below code, the target attribute specifies the target browsing context in which the page is to be opened.

<a href="page.html" target="TargetName">Click me</a>

查看此 JSBin

希望这有点帮助。阅读HTML规范以了解有关浏览上下文的更多信息。

Hope this was somewhat helpful. Read the HTML Spec to understand more about browsing contexts.

这篇关于名称已在控制台中定义!?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆