[英]How use prototypical inheritance in controllers in node.js
我需要使一個超級函數繼承其他函數的this,並使該另一個函數繼承超級函數的方法,這可能嗎?
說明:
我有我的BookingController
,我想讓Controller
函數繼承this.ME
屬性:
BookingController.js:
var Controller = require('../api/Controller');
function BookingController() {
Controller.call(this);
this.ME = 'something here';
}
BookingController.prototype = new Controller;
BookingController.constructor = BookingController;
Controller.js:
function Controller() {
console.log(this); // EMPTY
};
Controller.prototype.myMethod = function() {
// Should work if BookingController try to access.
}
但是什么也沒有發生,沒有錯誤,並且BookingController
找不到我的myMethod
而Controller找不到我的this.ME
您使用了錯誤的繼承模型。 我建議您使用類似
BookingController.prototype = Object.create(Controller.prototype);
BookingController.prototype.constructor = BookingController;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.