Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is console noting that I changed my prototype before I do?

Tags:

javascript

I am learning about Javascript prototypes and made a Fiddle (http://jsfiddle.net/3MuZa/1/) with this javascript:

function Animal(name, sound) {
    this.name = name;
    this.sound = sound;
}

var dog = new Animal("Dog", "Bark");
console.debug(dog.__proto__);

Animal.prototype.makeSound = function() {console.log(this.sound);};

Interestingly, ​console.debug(dog.__proto__); reveals that makeSound is a method of the prototype of the Animal class.

However, I add that method to the prototype in a later line. Why is Console noting that the prototype has a makeSound method if the control flow hadn't gotten to it yet in my code?

like image 490
John Hoffman Avatar asked Apr 21 '12 23:04

John Hoffman


1 Answers

The console is "live". even if you do log it first, it will reflect later changes.

As far as i know, it behaves like this on objects, functions, arrays. But if you log strings, booleans and numbers, they print what they mean at the time they were logged.

like image 102
Joseph Avatar answered Nov 02 '22 05:11

Joseph