国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

javascript中的3種繼承實(shí)現(xiàn)方法

2019-11-20 10:40:42
字體:
供稿:網(wǎng)友

使用Object.create實(shí)現(xiàn)類式繼承

下面是官網(wǎng)的一個(gè)例子

//Shape - superclassfunction Shape() { this.x = 0; this.y = 0;}Shape.prototype.move = function(x, y) {  this.x += x;  this.y += y;  console.info("Shape moved.");};// Rectangle - subclassfunction Rectangle() { Shape.call(this); //call super constructor.}Rectangle.prototype = Object.create(Shape.prototype);var rect = new Rectangle();rect instanceof Rectangle //true.rect instanceof Shape //true.rect.move(1, 1); //Outputs, "Shape moved."

此時(shí)Rectangle原型的constructor指向父類,如需要使用自身的構(gòu)造,手動(dòng)指定即可,如下

Rectangle.prototype.constructor = Rectangle;

使用utilities工具包自帶的util.inherites

語法

util.inherits(constructor, superConstructor)
例子

const util = require('util');const EventEmitter = require('events');function MyStream() {  EventEmitter.call(this);}util.inherits(MyStream, EventEmitter);MyStream.prototype.write = function(data) {  this.emit('data', data);}var stream = new MyStream();console.log(stream instanceof EventEmitter); // trueconsole.log(MyStream.super_ === EventEmitter); // truestream.on('data', (data) => { console.log(`Received data: "${data}"`);})stream.write('It works!'); // Received data: "It works!"

也很簡(jiǎn)單的例子,其實(shí)源碼用了ES6的新特性,我們瞅一瞅

exports.inherits = function(ctor, superCtor) { if (ctor === undefined || ctor === null)  throw new TypeError('The constructor to "inherits" must not be ' +            'null or undefined'); if (superCtor === undefined || superCtor === null)  throw new TypeError('The super constructor to "inherits" must not ' +            'be null or undefined'); if (superCtor.prototype === undefined)  throw new TypeError('The super constructor to "inherits" must ' +            'have a prototype'); ctor.super_ = superCtor; Object.setPrototypeOf(ctor.prototype, superCtor.prototype);};

其中Object.setPrototypeOf即為ES6新特性,將一個(gè)指定的對(duì)象的原型設(shè)置為另一個(gè)對(duì)象或者null

語法

Object.setPrototypeOf(obj, prototype)
obj為將要被設(shè)置原型的一個(gè)對(duì)象
prototype為obj新的原型(可以是一個(gè)對(duì)象或者null).

如果設(shè)置成null,即為如下示例

Object.setPrototypeOf({}, null);
感覺setPrototypeOf真是人如其名啊,專門搞prototype來玩。
那么這個(gè)玩意又是如何實(shí)現(xiàn)的呢?此時(shí)需要借助宗師__proto__

Object.setPrototypeOf = Object.setPrototypeOf || function (obj, proto) { obj.__proto__ = proto; return obj; }

即把proto賦給obj.__proto__就好了。

使用extends關(guān)鍵字

熟悉java的同學(xué)應(yīng)該非常熟悉這個(gè)關(guān)鍵字,java中的繼承都是靠它實(shí)現(xiàn)的。
ES6新加入的class關(guān)鍵字是語法糖,本質(zhì)還是函數(shù).

在下面的例子,定義了一個(gè)名為Polygon的類,然后定義了一個(gè)繼承于Polygon的類 Square。注意到在構(gòu)造器使用的 super(),supper()只能在構(gòu)造器中使用,super函數(shù)一定要在this可以使用之前調(diào)用。

class Polygon { constructor(height, width) {  this.name = 'Polygon';  this.height = height;  this.width = width; }}class Square extends Polygon { constructor(length) {  super(length, length);  this.name = 'Square'; }}

使用關(guān)鍵字后就不用婆婆媽媽各種設(shè)置原型了,關(guān)鍵字已經(jīng)封裝好了,很快捷方便。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 利辛县| 洛隆县| 廊坊市| 太仆寺旗| 巴彦淖尔市| 农安县| 龙里县| 闽清县| 宁明县| 扎兰屯市| 楚雄市| 阳泉市| 五家渠市| 嘉祥县| 泽库县| 华池县| 定边县| 灵台县| 铜梁县| 巴东县| 廊坊市| 宁乡县| 和政县| 攀枝花市| 新泰市| 廉江市| 平塘县| 巴塘县| 乌鲁木齐县| 景泰县| 中江县| 泸州市| 寿光市| 磐石市| 观塘区| 齐河县| 息烽县| 边坝县| 宁晋县| 灌云县| 华容县|