0
function Foo() {  
    alert(this === a);//return false!   **why this is not equal a?**  
}  
var a = new Foo();//create a new object

foo is a constructor,why this in the 'Foo' is not equal with 'a'.

2 Answers 2

6

The assignment to the variable a has not yet happened when you're inside the constructor. The sequence of events is:

  1. Create a new object
  2. Run the constructor
  3. Assign the new object to the variable a.
Sign up to request clarification or add additional context in comments.

3 Comments

@KooiInc alerts true?i run the code in firebug, it alerts false.
@ray: run the exact code string I gave in my previous comment in whatever browser. My code string differs from yours - alert(this*.name* === a);.
@KooiInc - the only reason your code alerts true is because both this.name and a are undefined. That isn't accomplishing anything.
-1

i think "this" referring to as a object, but "this.a" is true as that is the object

2 Comments

There is no this.a here. So i don't really get what you're trying to say.
well it look to me as being as ray was trying to check for equality on "a" as "this" was an object of objects

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.