Question:

The prototype property of constructor functions can also be set to add methods to objects created by constructor functions. JavaScript functions are objects and the prototype property can be used to add methods to the objects that are created by constructor functions.

function Dog(name) {
  this.name = name;
}

Dog.prototype = {
  constructor: Dog,
  bark: function () { return "ruff ruff" }
}

var rover = new Dog("red rover");
console.log(rover.name);  // returns "red rover"
What does the following line print to the console?
console.log(rover.bark());

Answer:

"ruff ruff"

The prototype design pattern is used to JavaScript to give methods to objects created by constructor functions.


Keywords:

© 2017 QuizBucket.org