Exploring Objects : Function Constructor

by Cristi Salcescu

JavaScript

function Timer(callback){
  this.token = 0;
}
Timer.prototype = Object.freeze({
  start : function() {},
  stop : function() {}
});

let timer = new Timer();

console.log(timer.__proto__ === Timer.prototype);


function newTimer(){
  let newObj = Object.create(Timer.prototype);
  let returnObj = Timer.call(newObj, arguments);
  if(returnObj) return returnObj;
    
  return newObj;
}

let otherTimer = newTimer();
console.log(otherTimer)