jquery - JavaScript new keyword and objects scopes -


Later today, I was scrolling through ejhon.com slides and I found the following:

Give this code

  doing the function () {this.myvar = true; } Katana (); Console.info (MyWor);  

The last time I compiled the code, I thought that myworld is linked to the function. Basically, this window is attached to objects, which pollutes the global namespace.

I returned to my projects, which use all the same approach. A little different ways

  function katana () {this.myvar = true; } Var xyz = new comment (); Console.info (MyWor);  

I have a function object and instead of executing the function, I make a new example of this (I'm not really sure what is happening). I use those values ​​through the prototype methods to store values ​​using XYZ and to do some work.

I am surprised that when I do some debugging with firebug, XYZs do not exist. There is no variable attached to the window object. Why?

I did some more debugging and connected to the XIZ object window> object, but this is not clear in the dom and there is no trace in it. There is nothing new in the debugging window, with a call called 'scopchain' and a value of xyz objects in it.

OK, what's going on below? Is this a good method with which I should stick or would I seek an alternative? I have noticed some questions and answers, I am mainly looking at what this method does in the background.

"post-text" itemprop = "text">

When you call new () , then javascript to katana function Is called this with a new empty object. Once it returns, it is probably started, the new object (or whatever katana returns, until it is an object) is established so that its 'Prototype') is similar to the prototype of the katana function.

Yes, this was the first time I could not understand a lot through it. There is an exaggerated version, you say that new katana () creates JavaScript and will be an katana example and the katana function lets it start is . You say that Cut () , and this is the last object in the call stack which is used in an instance method call. (If your caller was x.foo , it was == x .) If there is no such thing, then it looks like it Is window .

Why xyz is not showing, you have declared the variable with var . This scope changes if you have got rid of it, then you will see a window.xyz


Comments

Popular posts from this blog

Eclipse CDT variable colors in editor -

AJAX doesn't send POST query -

wpf - Custom Message Box Advice -