Javascript question

javascript

#1

my question is why this==penguin false?
I really cant get it. am i missing something?

function Animal(name, numLegs) {
    //console.log(this) //{}
    this.name = name;
    this.numLegs = numLegs;

       // console.log(this) //{ name: 'Captain Cook', numLegs: 2 }
      //console.log(penguin) //{ name: 'Captain Cook', numLegs: 2 }
     //console.log(typeof(this)) //object
    //console.log(typeof(penguin)) //object
   //console.log(this===penguin)//flase
    console.log(this==penguin);//flase

 }



// create the sayName method for Animal


Animal.prototype.sayName = function () {
    //console.log("Hi my name is"+ " " + this.name);
   //console.log(this) //{ name: 'Captain Cook', numLegs: 2 }
  //console.log(this===penguin) //true
};

// provided code to test above constructor and method
var penguin = new Animal("Captain Cook", 2);
penguin.sayName();

#2

You're still in the constructor, nothing has been assigned to that variable yet


#4

At first i want to say thank you very much :slight_smile:
Actullly console.log(typeof(penguin)) //undefined

but i was using codecademy labs and browser is showing me like

first undefined
second object
   
first object
second object
   
first object
second object
   
first object
second object

on this code

function Animal() {
    console.log("first", typeof penguin); // undefined
}
var penguin = new Animal();
console.log("second", typeof penguin);

undefined only for the first time.
but in jsbin it was continuously undefined.

can you tell me why so?


#5

Printing something before and after defining it should first say it's not there and then that it is, shouldn't it?


#6

yeah i did.but same thing i did in other environment too.
the result should be same but it was not


#7

Clearly you didn't do that in the other environment. You probably replaced it between each time.


#8


what i just did click on run


#9

And because the variable is no longer there you can conclude that it was removed each time you did so.

Use a loop if you want to run it all as the same program.


#10

you are right but this time again heppend same thing.
codecademy labs:

first undefined
first undefined
first undefined
first undefined
first undefined
second object
**2nd time run**
first object
first object
first object
first object
first object
second object

and jsbin

"first"
"undefined"
"first"
"undefined"
"first"
"undefined"
"first"
"undefined"
"first"
"undefined"
"second"
"object"
**2nd time**
"first"
"undefined"
"first"
"undefined"
"first"
"undefined"
"first"
"undefined"

i understand what you want to say.but my question still same.

same code on same environment at same time but both are different.whenever i ran 2nd time cclab take as object and jsbin continuously as undefined?why so?


#11

That output is meaningless without showing what you did.

But the answer is still going to be that until you define a variable, it won't be there, and after you do, it will.

You're probably just printing it out ten times, then defining it. Why would anything change during that? Think about what it is you're testing and test only that.


#12

i'm very sorry for that

yeah! you are right :slight_smile: . now i've understand.

thank you very much for precious time :slight_smile: