Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

what is the use of a constructor method in javascript

/* A constructor in Javascript is used to create a new object and set values
for any existing object properties*/

class Animal{
  
  constructor (name, height, weight){
    console.log("created animal named", name)
  }
  nameLength() {
    return name.length;
    
  }
  
}
 var dog = new Animal("Fido", 25, 90);
 var fish = new Animal("Goldie", 2, .02);

	console.log(dog.nameLength);
 
PREVIOUS NEXT
Tagged: #constructor #method #javascript
ADD COMMENT
Topic
Name
5+6 =