‘this’ 不会绑定的

In order to start asking the question, what does the key word this refer to, we really need a code example where the keyword this appears. Here we have it appearing inside a function which is generally how you are going to see it. So if you notice the keyword this appearing somewhere in a function, what would you imagine it is bound to? let’s go through a quick list of things that people frequently think it’s bound to although it usual is not. So first of all you might see this function definition here. Highlight it in blue, and you know that when your interpreter hits it, it’s going to create a function object in memory. You may then conclude that the keyword this should refer to that function object, but it doesn’t. As a next step sometimes people conjecture that an instance of that function is created and the keyword this refers to it. Now, in certain circumstances that’s true. But in order to really get on board with that definition, I would have to ask, what is it that you imagine a new instance of this function would be? I mean, strictly speaking, what is this object in your mind? Later on we’re going to go on to examine how this could be considered true but it’s actually rarely the case that you’re going to see code that looks like this. So let’s say, for the sake of argument. But this is basically an error, generally speaking. You might then think that in order for the keyword this to mean anything it must be in a function that is contained within some other object as a property. Here was have an object ob2. That has a method called method that stores a reference to that same function object, so that object creates an object in memory and maybe that in memory object, where the function is a property, would be the thing that the keyword this refers to. This is actually one of the most popular misconceptions. But it’s also not the case. And we’ll see why in just a minute. The easiest way to think about this, though, is what if that same function was a property of two different objects? It doesn’t stand to reason that the keyword, this, would be forced to choose somehow between the two of them. Taking it a step further, we could then claim that the keyword this will appear inside a function. And that function must appear inside the curly braces of some object literal. Or some other form of defining a function. So that object literal that surrounds the function definition might create an in memory object and that could be what we’re referring to when we say the keyword this. But this is also not correct. Lastly, we know we will eventually call this function, and as we pass values in to be bound to the positional parameters of the function, we could imagine that that invocation creates a set of bindings, or a scope. That scope must be modeled somewhere in memory. And maybe it’s represented as an object where a maps to three in this case, and b maps to four. We would either call this an execution context, or a scope. But this is also not what the keyword this refers to. As we recall from our earlier conversation about scopes, it’s not the case that JavaScript gives you any memory reference access to these constructs.


Leave a Reply

Your email address will not be published. Required fields are marked *