前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >JS中ES6继承

JS中ES6继承

作者头像
贵哥的编程之路
发布2020-10-28 10:42:15
1.5K0
发布2020-10-28 10:42:15
举报
文章被收录于专栏:用户7873631的专栏
代码语言:javascript
复制
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Document</title>
</head>
<body>
	<script type="text/javascript">
		/*function Person(myName, myAge)
		{
			this.name = myName;
            this.age = myAge;
		}
		 Person.prototype.say =  function () {
            console.log(this.name, this.age);
        }
        function Student(myName, myAge, myScore)
        {
        	Person.call(this,myName,myScore);//在子类中通过call/apply方法借助父类的构造函数
        	//1`改变函Person的this指向,使之指向Student;
        }
        Student.prototype=new Person();
        Student.prototype.coustructor=Student;
        //这两句代码的意思是:我直接说指向把。student的实例对象--》student的构造函数--》然后是person的实例对象--》 Student.prototype.coustructor指向了student的构造函数。
        let stu = new Student("zs", 18, 99);
        stu.say();
       */
      class Person{
            constructor(myName, myAge){
                // this = stu;
                this.name = myName; // stu.name = myName;
                this.age = myAge; // stu.age = myAge;
            }
            say(){
                console.log(this.name, this.age);
            }
        }
         /*
        1.在ES6中如何继承
        1.1在子类后面添加extends并指定父类的名称
        1.2在子类的constructor构造函数中通过super方法借助父类的构造函数
        */
        // 以下代码的含义: 告诉浏览器将来Student这个类需要继承于Person这个类
        class Student extends Person
        {
        	constructor(myName, myAge, myScore)
        	{
        		// Person.call(this, myName, myAge);相当于
        		super(myName,myAge);
        		this.score=myScore;
        	}
        	study(){
                console.log("day day up");
            }
        }
        let stu = new Student("zs", 18, 98);
        stu.say();
	</script>
</body>
</html>
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020/09/11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档