Javascript Object Question 2

Introduction

What is the output of the following code?

let Animal = function(thename) {
    this.fur=true;//w  w w .  ja  v a2s  .c o  m
    this.scales=false;
    this.name='Generic'; 
    if (thename){
         this.name=thename;
    }
};

let cat = new Animal('cat');
let unknown_animal = new Animal();  

console.log(cat.name);
console.log(unknown_animal.name);   


cat
Generic

Note

let Animal = function(thename) {
    this.fur=true;/*from  www . java 2 s. com*/
    this.scales=false;
    this.name='Generic'; 
    if (thename){
         this.name=thename;
    }
};

let cat = new Animal('cat');
let unknown_animal = new Animal();    // the default name is 'Generic'

console.log(cat.name); // "cat"
console.log(unknown_animal.name);    // "Generic"



PreviousNext

Related