JavaScript原型和原型链学习

一、引用类型-对象

1、普通声明变量
	//普通声明变量
	let student = new Object();
	student.id=100
	student.name="JSON"
	student.sayhi =function(){
		console.log("我叫"+this.name+",id是"+this.id)
	}
	student.sayhi() //结果我叫JSON,id是100
	console.log(student.name) //结构JSON
2、字面量声明
	///字面量声明
let student ={
	id :200,
	name:"tom",
	sayhi: function(){
		console.log("我叫"+this.name+",id是"+this.id)
	}
}
student.sayhi() //结果我叫tom,id是200
console.log(student.name) //结果tom
3、构造函数 在构造函数中定义类的属性和方法
//构造函数  在构造函数中定义类的属性和方法
	function student3(id,name){
	this.id = id,
	this.name = name,
	this.sayhi = function(){
		console.log("我叫"+this.name+",id是"+this.id)
	}
}

let student = new student3(300,"liah")
let student1 = new student3(100,"wnagyw")
let student2 = new student3(320,"lssdh")
student.sayhi() //我叫liah,id是300
student1.sayhi() //我叫wnagyw,id是100
student2.sayhi() // 我叫lssdh,id是320

二、原型应用

//prototype称为原型,类似java里的static

Student3.prototype.count =100; 
 //原型变量  -- java里叫静态变量
 
Student3.prototype.study = function(){
	console.log("共享学习空间")
}  //原型方法   静态方法

let student = new Student3(300,"liah")
let student1 = new Student3(100,"wnagyw")
let student2 = new Student3(320,"lssdh")

student.study()
student1.study()
student2.study()

student.sayhi()
student1.sayhi()
student2.sayhi()

console.log(student.sayhi == student1.sayhi)  //不同实例对象的普通方法的地址是不一样的  开辟了新空间
console.log(student.study == student1.study)  //不同实例对象的原型方法的地址是一样的 都指向一个空间,节省了空间

console.log(student)
console.log(student1)
console.log(student2)
典型案例

  • 10
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

安风\(`Δ’)/

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值