javascript类的原型语法错误

时间:2017-08-06 05:48:46

标签: javascript class

class Rectangle {
    constructor(w, h) {
        this.w = w;
        this.h = h;
    }
}

Rectangle.prototype.area = funcion() {
    var area = this.w * this.h;
    return area;
}

class Square extends Rectangle {
   constructor(w){
       this.w=w;
       this.h=w;
   }
}

当我尝试运行此代码时,它会出现以下错误:

  

solution.js:10

     

Rectangle.prototype.area = funcion(){

     

^ SyntaxError:意外的令牌{

1 个答案:

答案 0 :(得分:1)

此行Rectangle.prototype.area中有拼写错误。您的拼写错误functionfuncion。只需更新它,代码就不会给你语法错误。

class Rectangle {
    constructor(w, h) {
        this.w = w;
        this.h = h;
    }
}

Rectangle.prototype.area = function() {
    var area = this.w * this.h;
    return area;
}

class Square extends Rectangle {
   constructor(w){
       this.w=w;
       this.h=w;
   }
}