The principle and simple usage of JS prototype

  • In JS, each object has its prototype object, and it can use all the methods on its prototype object
  • Obtain the prototype object method through the object _proto_, or get the prototype through the prototype attribute
  • Role: add methods to the prototype to achieve reuse, etc.
let data = new Date();
Date.prototype.formate = function(){
    
    
	let year = this.getFullYear();
	let month = this.getMonth()+1;
	let date= this.getDate();
	return `${
      
      year}${
      
      month}${
      
      date}`
}

insert image description here

Guess you like

Origin blog.csdn.net/Beatingworldline/article/details/120830557