JavaScript新的关键字和对象范围 [英] JavaScript new keyword and objects scopes
问题描述
今天晚些时候,我浏览了ejhon.com幻灯片,发现了以下内容:
给这段代码
函数katana(){
this.myvar = true;
}
katana();
console.info(myvar);
过去我编译代码的那一刻,我认为myvar被附加到katana函数中。实际上,它会附着到窗口对象,这会污染全局命名空间。
我返回到了我的项目,这些项目都使用相同的方法..稍有不同
函数katana(){
this.myvar = true;
}
var xyz = new katana();
console.info(myvar);
我有一个函数对象,而不是执行函数,我只是创建它的一个新实例我并不确定发生了什么)。然后我使用xyz来存储值并通过原型方法使用这些值来完成一些工作。
令我感到惊讶的是,当我使用FireBug进行一些调试时,xyz不是存在。没有变量附加到窗口对象。为什么?
我做了一些更多的调试,xyz对象连接到window> object,但是在DOM中它不明显,没有任何痕迹。在调试窗口中还有一些新东西,一个叫做scopechain的节点带有一个调用,并且具有xyz对象的值。
好的,下面发生了什么?这是一种我应该坚持的好方法还是我应该寻找替代方案?
我已经看过一些问题和答案,我主要在寻找这种方法在后台执行的操作。 p>当你说 new katana()
时,Javascript会调用 katana
函数,并将一个新的空白对象作为此
。一旦它返回,大概初始化,新对象(或任何 katana
返回,只要它是一个对象)被设置为使其原型(它的对象)将继承字段等)与 katana
函数的原型相同。
是的,当我第一次经历它时,这很有意义。过度简化的版本是,你说 new katana()
,并且Javascript创建了一个 katana
实例,并让 katana
函数初始化它。你说 katana()
,并且这个
基本上是调用堆栈中最后一个在实例中使用的对象方法调用。 (如果你的调用者是 x.foo
, this == x
。)如果没有这样的对象,看起来 this
与 window 。
相同至于为什么 xyz
没有出现,你用 var
声明变量。这改变了范围。如果你摆脱了这一点,你会看到 window.xyz
。
Later today, I was scrolling through ejhon.com slides and I found out the following:
Give this code
function katana () {
this.myvar = true;
}
katana ();
console.info (myvar);
Past the moment I compiled the code, I thought that myvar is attached to the katana function. Actually, it gets attached to the window objects, which pollutes the global namespace.
I returned to my projects, which all uses the same approach.. a little bit differently
function katana () {
this.myvar = true;
}
var xyz = new katana();
console.info (myvar);
I have a function object and instead of executing the function, I just create a new instance of it (I'm not actually quite sure what's happening). I then use xyz to store values and use those values through prototyped methods to do some jobs.
What surprised me is when I did some debugging with FireBug is that xyz is not existent. There is no variables attached to window object. Why?
I did some more debugging and the xyz object is attached to window > object, but in the DOM it's not apparent and has no traces. There is also something new in the debugging window, a node called 'scopechain' with a call and that has the values of the xyz object.
Okay, what's happening underneath? Is that a good method that I should stick with or shall I look for an alternative? I have looked at some questions and answers, I'm mainly looking for what this method do in the background.
When you say new katana()
, Javascript calls the katana
function, with a new blank object as this
. Once it returns, presumably initialized, the new object (or whatever katana
returns, as long as it's an object) is set up so that its 'prototype' (the object that it'll inherit fields and such from) is the same as the katana
function's prototype.
Yeah, that didn't make much sense either the first time i went through it. The oversimplified version is, you say new katana()
, and Javascript creates what'll be a katana
instance and lets the katana
function initialize it. You say katana()
, and this
is basically the last object in the call stack to have been used in an instance method call. (If your caller was x.foo
, this == x
.) If there's no such object, seems this
is the same as window
.
As for why xyz
isn't showing up, you declared the variable with var
. That changes the scope. If you got rid of that, you'd see a window.xyz
.
这篇关于JavaScript新的关键字和对象范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!