美文网首页
super關鍵字

super關鍵字

作者: 叹玖 | 来源:发表于2018-07-28 17:00 被阅读0次

super关键字用于访问和调用一个父对象上的函数。
在构造函数中使用时,super必须放在this之前,否则,会报错。

1.在类中使用super

class Polygon {
    constructor(height, width) {
        this.name = 'Polygon';
        this.height = height;
        this.width = width;
    }
    sayName() {
        console.log('Hi, I am a ', this.name + '.');
    }
}

class Square extends Polygon {
    constructor(length) {
        //this.height;  //this不允許使用在super前
        super(length, length);
        this.name = 'Square';
    }

    get area() {
        return this.height * this.width;
    }

    set area(value) {
        //this.area = value;
        console.log(value);
    }
}

var res = new Square(2);
console.log(res,res.area); //Square { name: 'Square', height: 2, width: 2 } 4
res.sayName(); //Hi, I am a  Square.

2. 调用父类的静态方法

class Human {
    constructor() {}
    static ping() {
        return 'ping';
    }
}

class Computer extends Human {
    constructor() {}
    static pingpong() {
        return super.ping() + ' pong';
    }
}
console.log(Computer.pingpong());  //ping pong

3.不能使用delete操作符删除super上的属性
class Base {
constructor() {}
foo() {}
}
class Derived extends Base {
constructor() {}
delete() {
 delete super.foo;
}
}

new Derived().delete(); 
// ReferenceError: invalid delete involving 'super'.

4.当使用 Object.defineProperty 定义一个属性为不可写时,super将不能重写这个属性的值。

相关文章

  • super關鍵字

    super关键字用于访问和调用一个父对象上的函数。在构造函数中使用时,super必须放在this之前,否则,会报错...

  • AVFoundation QRCode 掃描範圍

    關鍵字: RectOfInterest

  • 關鍵字提煉

    制作教材的时候,关键字提取的成功与否,是影响教材成败的很大的关键。因此平常要多练习关键字的提取,学着用2到4个字,...

  • 管理学

    一、關鍵字解釋 administrative model 行政模式 A decision-making model...

  • 介绍 |《五大关键数字力》不懂财报,也能轻松选出赚钱绩优股

    《不懂財報,也能輕鬆選出賺錢績優股:五大關鍵數字力》 MJ林明樟 不懂財報,也能輕鬆選出賺錢績優股:五大關鍵數字力...

  • SEO基础

    關鍵詞排名查詢:百度指數 關鍵詞挖掘{ 1.百度搜索框 2.百度搜索下拉欄 3.百度指數:【關鍵詞搜索趨勢,相關性...

  • 關鍵詞

    有些經歷會讓某些字眼變得無比敏感。 比如說: Tango 地震 竊聽風雲 正 Sir 樂譜 ......

  • Wood Cut

    ===================== 解題思路 ===================== 關鍵是找到 bi...

  • Jruby+Rails初體驗

    此篇關鍵字 Jruby (跑起來真的有夠慢的) rmagick4j, RMagick jdbcsqlite3 ca...

  • 設計思維(Design Thinking)

    設計思維五步驟 ,讓世界更美好 1.共情 關鍵詞:接觸,訪問,共感 2.定義 關鍵詞:發現問...

网友评论

      本文标题:super關鍵字

      本文链接:https://www.haomeiwen.com/subject/vdwomftx.html