JS-面向对象编程
时间:2021-06-02 14:58:58
收藏:0
阅读:0
面向对象都拥有,类和对象
对象:是一个实例,是类的具体表现
JS、Java、C#等一些列都是面向对象的语言,然后JS和其他的有些不同,需要换一下思维
原型
与继承类似
‘use strict‘;
let Person={
name:"yp",
age:20,
score:0,
email:"1351414677@qq.com",
run:function(){
console.log(this.name +" run ..");
}
};
?
?
let xiaoying={
name:‘xiaoying‘
};
//小樱的原型是人
xiaoying.__proto__=Person;
function Student(name){
this,name=namme;
}
//给Student新增一个方法
Student.prototype.hello=function(){
alert(‘hello‘);
}
class
在ES6中引入的class关键字
-
定义一个类,属性,方法
class Student{
constructor(name){
this.name=name;
}
hello(){
alert(‘hello‘);
}
}
?
let xiaoming=new Student(‘xiaoming‘);
let xiaoying=new Student(‘xiaoying‘);
xiaoying.hello();
-
继承
class Student{
constructor(name){
this.name=name;
}
hello(){
alert(‘hello‘);
}
}
?
class pupil extends Student{
constructor(name,grade){
super(name);
this.grade=grade;
}
myGrade(){
alert(‘是一名小学生‘);
}
}
本质:还是查看对象原型
原型链
评论(0)