JS原型的原理及简单用法

  • JS中,每一个对象都有它的原型对象,它可以使用自己原型对象上的所有方法
  • 通过对象的_proto_获取原型对象方法,或通过prototype属性拿到原型
  • 作用:给原型添加方法 从而实现复用等
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}`
}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/Beatingworldline/article/details/120830557