<div class="article-content">
<h5 class="heading">es6 class基础用法</h5>
<p>以前的JavaScript没有类的概念,它是基于原型的面相对象的语言。原型对象的特点就是将自身属性共享给新对象。我们先看一下下面的代码实现。</p>
<pre class="blockcode"><code class="hljs javascript copyable"> <span class="hljs-comment">//常规写法</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Person</span>(<span class="hljs-params">name,age</span>) </span>{
<span class="hljs-keyword">this</span>.name = name;
<span class="hljs-keyword">this</span>.age = age;
}
Person.prototype.sayInfo = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">`<span class="hljs-subst">${<!-- --><span class="hljs-keyword">this</span>.name}</span>是<span class="hljs-subst">${<!-- --><span class="hljs-keyword">this</span>.age}</span>岁`</span>)
}
<span class="hljs-keyword">const</span> liLei = <span class="hljs-keyword">new</span> Person(<span class="hljs-string">'LiLei'</span>,<span class="hljs-number">20</span>)
liLei.sayInfo()
<span class="hljs-comment">//LiLei是20岁</span>
<span class="copy-code-btn">复制代码</span></code></pre>
<p>这种常规约定是以大写字母开头来表示一个构造器(大写开头非官方),可以直接定义函数,也可以通过prototype来扩展函数。这种实现跟java比,实现类的方案太特别了,下面我们看一下es6的类的实现方式:</p>
<pre class="blockcode"><code class="hljs javascript copyable"> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span></span>{ <span class="hljs-comment">//定义了一个名字为Person的类</span>
<span class="hljs-keyword">constructor</span>(name,age){ <span class="hljs-comment">//constructor是一个构造方法,用来接收参数</span>
<span class="hljs-keyword">this</span>.name = name; <span class="hljs-comment">//this代表的是实例对象</span>
<span class="hljs-keyword">this</span>.age = age;
}
sayInfo(){
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">`<span class="hljs-subst">${<!-- --><span class="hljs-keyword">this</span>.name}</span>是<span class="hljs-subst">${<!-- --><span class="hljs-keyword">this</span>.age}</span>岁`</span>)
}
}
<span class="hljs-keyword">const</span> liLei = <span class="hljs-keyword">new</span> Person(<span class="hljs-string">'LiLei'</span>,<span class="hljs-number">21</span>)
liLei.sayInfo()
<span class="copy-code-btn">复制代码</span></code></pre>
<p>由下面代码可以看出类实质上就是一个函数。类自身指向的就是构造函数。所以可以认为ES6中的类其实就是构造函数的另外一种写法!下面的代码可以证明这一点</p>
<pre class="blockcode"><code class="hljs javascript copyable"><span class="hljs-built_in">console</span>.log(<span class="hljs-keyword">typeof</span> Person);<span class="hljs-comment">//function</span>
<span class="hljs-built_in">console</span>.log(Person===Person.prototype.constructor);<span class="hljs-comment">//true</span>
<span class="copy-code-btn">复制代码</span></code></pre>
<h5 class="heading">类的继承</h5>
<p>JavaScript中的类同样可以像java一样,可以继承某个类,其中被继承的类称为父类,而继承父类的被称为子类。子类可以有自己的函数和构造器,当子类中存在父类相同的方法时,则该方法不会从父类继承,而使用子类的方法。</p>
<pre class="blockcode"><code class="hljs javascript copyable"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Student</span> </span>{
<span class="hljs-keyword">constructor</span>(name){
<span class="hljs-keyword">this</span>.name = name
}
sayName(){
<span class="hljs-built_in">console</span>.log(<span class="hljs-keyword">this</span>.name)
}
testFn(){
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'我是父类的函数!'</span>)
}
}
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Worker</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Student</span></span>{
sayWork(){
<span class="hljs-built_in">console</span>.log(<span class="hljs-keyword">this</span>.name)
}
testFn(){
<span class="hljs-built_in">console</span>.log(<span class="hljs-s |
|