ES6 class 类

论坛 期权论坛     
选择匿名的用户   2021-6-2 18:27   100   0
<h1>1、定义 “类”</h1>
<p style="margin-left:0cm;">之前都是使用new function来定义一个函数的类型,现在我们可以使用class关键字来定义一个函数类</p>
<h2 style="margin-left:0cm;">1.1 普通函数定义的类</h2>
<pre class="blockcode"><code class="language-javascript">function People(name, age, sex) {
    this.name &#61; name;
    this.age &#61; age;
    this.sex &#61; sex;
}
People.prototype.play &#61; function () {
    console.log(&#96;我是${this.name},今年${this.age}岁,我喜欢打游戏&#96;)
}
People.prototype.sing &#61; function () {
    console.log(&#96;${this.name}还喜欢唱个歌&#96;)
}
var zhangSan &#61; new People(&#34;张三&#34;, 25, &#34;男&#34;)
zhangSan.play();
zhangSan.sing();</code></pre>
<p><img alt="" height="60" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-792918b7bf0c1712ed81073ae050628d.png" width="319"></p>
<h2>1.2 class定义的类</h2>
<pre class="blockcode"><code class="language-javascript">class People {
    constructor(name, age, sex) {
        this.name &#61; name;
        this.age &#61; age;
        this.sex &#61; sex;
    }
    play() {
        console.log(&#96;我是${this.name}今年${this.age}岁,我喜欢打游戏&#96;)
    }
    sing() {
        console.log(&#96;${this.name}还喜欢唱歌&#96;)
    }
}
var lisi &#61; new People(&#34;李四&#34;, 23, &#34;女&#34;);
lisi.play();
lisi.sing();</code></pre>
<p><img alt="" height="54" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-547a44b5099b4b95e89b6ac62ffb88d0.png" width="322"></p>
<p><img alt="" height="313" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-102a6008cea71dbe3ff5e270d913965c.png" width="470"></p>
<p style="margin-left:0cm;"><strong><span style="color:#ff0000;">虽然</span><span style="color:#ff0000;">es6</span></strong><strong><span style="color:#ff0000;">增加了</span><span style="color:#ff0000;">class</span></strong><strong><span style="color:#ff0000;">关键字,但是</span><span style="color:#ff0000;">JavaScript</span></strong><strong><span style="color:#ff0000;">中还是没有类的概念!依然是基于面向对象,而不是真正的面向对象,基于原型链来模型实现</span><span style="color:#ff0000;">class</span></strong><strong><span style="color:#ff0000;">,机理和普通函数定义的类是一样的</span></strong></p>
<pre class="blockcode"><code class="language-javascript">console.log(xiaohong.singsong &#61;&#61;&#61; People.prototype.singsong) // true
console.log(xiaohong.hasOwnProperty(&#34;singsong&#34;)) // false
console.log(xiaohong.hasOwnProperty(&#34;name&#34;)) // true
</code></pre>
<p><img alt="" height="294" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-183a894c0fdd35349516e7cdb1983456.png" width="571"></p>
<h1> 2、继承</h1>
<h2>2.1普通函数的继承</h2>
<pre class="blockcode"><code class="language-javascript">function People(name, age, sex) {
    this.name &#61; name;
    this.age &#61; age;
    this.sex &#61; sex;
}
People.prototype.play &#61; function () {
    console.log(&#96;我是${this.name},今年${this.age}岁了,我喜欢打游戏&#96;);
}
People.prototype.sing &#61; function () {
    console.log(&#34;我花喜欢唱歌&#34;);
}

function Student(name, age, sex, id, score, school) {
    this.name &#61; name;
    this.age &#61; age;
    this.sex &#61; sex;
    this.id &#61; id;
    this.score &#61; score;
    this.school &#61; school;
}
Student.prototype &#61; new People();

Student.prototype.study &#61; function () {
    console.log(&#96;我的学校是${this.school},学号${this.id},成绩${this.score}&#96;);
}
var xiaoming &#61; new Student(&#34;小明&#34;, 22, &#34;男&#34;, &#34;100001&#34;, &#34;90&#34;, &#34;实验中学&#34;);
xiaoming.play();
xiaoming.study();</code></pre>
<p><img alt="" height="70" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-53a3914649d81fd08c2cb4f0c0fd07a3.png" width="283"></p>
<h2>2.2继承的原理</h2>
<p><img alt="" height="285" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-730403a5c2381d3ec34e2d98221bff61.png" width="568"></p>
<h2>2.3ES6实现继承</h2>
<pre class="blockcode"><code class="language-javascript">class People {
    constructor(name, age, sex) {
        this.name &#61; name;
        this.age &#61; age;
        this.sex &#61; sex;
    }
    play() {
        console.log(&#96;我是${this.name}今年${this.age}岁,我喜欢打游戏&#96;)
    }
    sing() {
        console.log(&#96;${this.name}还喜欢唱歌&#96;)
    }
}
class Student extends People {
    constructor(name, age, sex, id, score, school) {
        super(name, age, sex);
        this.id &#61; id;
        this.score &#61; score;
        this.school &#61; school;
    }
    study() {
        console.log(&#96;我的学校是${this.school},学号${this.id},成绩${this.score}&#96;);
    }
}
var xiaoming &#61; new Student(&#34;小明&#34;, 30, &#34;男&#34;, 10001, 89, &#34;实验中学&#34;)
xiaoming.play();
xiaoming.study();</code></pre>
<p><img alt="" height="49" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-94e66e5fba21b6d385dc460208bb86fe.png" width="317"></p>
<h1>3、constructor方法</h1>
<p style="margin-left:0cm;">constructor方法是类的默认方法,通过new命令生成对象实例时,自动调用该方法<span style="color:#ff0000;">。一个类必须有</span><span style="color:#ff0000;">co
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP