简体   繁体   English

有效的继承方式

[英]Efficient way to achieve inheritance

What will be the best way to get rid of repetitive code 什么是摆脱重复代码的最佳方法

let BaseErrorResponse = function(mes, rti, rsi, st) {
    return {
        "message": msg,
        "response_type_id": rti,
        "response_status_id": rsi,
        "status": st
    }
};


let InvalidParamResponse = function(mes, rti, rsi, st, ip) {
    return {
        "message": msg,
        "response_type_id": rti,
        "response_status_id": rsi,
        "status": st,
        "invalid_params": ip
    }
};


let SuccessResponse = function(msg, rti, rsi, st, data) {
    return {
        "message": null,
        "response_type_id": null,
        "response_status_id": null,
        "status": null,
        "data": {}
    }
};

You can just merge objects : 你可以合并对象

let BaseErrorResponse = function(mes, rti, rsi, st) {
    return {
        "message": msg,
        "response_type_id": rti,
        "response_status_id": rsi,
        "status": st
    }
};


let InvalidParamResponse = function(mes, rti, rsi, st, ip) {
    return Object.assign(BaseErrorResponse(mes, rti, rsi, st), {
        "invalid_params": ip
    });
};


let SuccessResponse = function(mes, rti, rsi, st, data) {
    return Object.assign(BaseErrorResponse(mes, rti, rsi, st), {
        "data": {}
    });
};

It might be a good idea to make these into actual constructors that inherit from each other, though. 但是,将这些构建成彼此继承的实际构造函数可能是个好主意。

function BaseErrorResponse(mes, rti, rsi, st) {
    this.message = msg;
    this.response_type_id = rti;
    this.response_status_id = rsi;
    this.status = st;
}

function InvalidParamResponse(mes, rti, rsi, st, ip) {
    BaseErrorResponse.call(this, mes, rti, rsi, st);
    this.invalid_params = ip;
}

InvalidParamResponse.prototype = Object.create(BaseErrorResponse.prototype);
InvalidParamResponse.prototype.constructor = InvalidParamResponse;

function SuccessResponse(mes, rti, rsi, st, data) {
    BaseErrorResponse.call(this, mes, rti, rsi, st);
    this.data = data;
}

SuccessResponse.prototype = Object.create(BaseErrorResponse.prototype);
SuccessResponse.prototype.constructor = SuccessResponse;

Well, as you're using ES2015 (aka ES6), seems like class might be an efficient option for you: 好吧,当你使用ES2015(又名ES6)时,看起来class对你来说可能是一个有效的选择:

class BaseErrorResponse {
    constructor(mes, rti, rsi, st) {
        this.message = msg;
        this.response_type_id = rti;
        this.response_status_id = rsi;
        this.status = st;
    }
}

class InvalidParamResponse extends BaseErrorResponse {
    constructor(mes, rti, rsi, st, ip) {
        super(mes, rti, rsi, st);
        this.invalid_params = ip;
    }
}

class SuccessResponse extends BaseErrorResponse {
    constructor(msg, rti, rsi, st, data) {
        super(null, null, null, null); // Why the nulls when you're passing
                                       // those args in?
        this.data = {};                // Didn't you mean = data here?
    }
}

Based on your reply to my comment on the question, that last one would be: 根据您对我对该问题的评论的回复,最后一个是:

class SuccessResponse extends BaseErrorResponse {
    constructor(msg, rti, rsi, st, data) {
        super(msg, rti, rsi, st);
        this.data = data;
    }
}

A little easier solution as for me is: 对我来说更简单的解决方案是:

var BaseErrorResponse = function(mes, rti, rsi, st) {
  return { mes, rti, rsi, st };
};

var InvalidParamResponse = function(mes, rti, rsi, st, ip) {
  var response = BaseErrorResponse(mes, rti, rsi, st);
  response.invalid_params = ip;
  return response;
};

var SuccessResponse = function() {
  var response = BaseErrorResponse(null, null, null, null);
  response.data = {};
  return response;
};

I have used TJ Crowder code as below and its working fine for me 我使用了如下TJ Crowder代码,它对我来说很好

'use strict';
class BaseErrorResponse {
    constructor(msg, rti, rsi, st) {
        this.message = msg;
        this.response_type_id = rti;
        this.response_status_id = rsi;
        this.status = st;
    }
}

class InvalidParamResponse extends BaseErrorResponse {
    constructor(mes, rti, rsi, st, ip) {
        super(mes, rti, rsi, st);
        this.invalid_params = ip;
    }
}

class SuccessResponse extends BaseErrorResponse {
    constructor(msg, rti, rsi, st, data) {
        super(msg, rti, rsi, st); // Why the nulls when you're passing
                                       // those args in?
        this.data = data;                // Didn't you mean = data here?
    }
}


(()=> {
    let sr = new SuccessResponse('Message', 1, 2, 3, {name: 'vivek'});
    console.log(sr);
})();

OUTPUT: OUTPUT:

test ) 测试)

node js-class-test.js 
SuccessResponse {
  message: 'Message',
  response_type_id: 1,
  response_status_id: 2,
  status: 3,
  data: { name: 'vivek' } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM