高效实现继承的方法

3
什么是消除重复代码的最佳方法?
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": {}
    }
};

SuccessResponse 中,你是不是应该写成 "data": data 而不是 "data": {}?另外,为什么要写 null - T.J. Crowder
哦哦哦...我错过了!这只是一个正在进行中的工作。我还在完成中... - Vivek Kumar
4个回答

4
你可以直接合并对象: Object.assign()
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": {}
    });
};

也许将它们变成实际的构造函数并相互继承会是一个好主意。
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;

1
关于你回答的第二部分:由于OP正在使用ES2015(基于let),所以只需使用class语法糖比自己编写代码更简单。(关于第一部分:如果期望/有用的话,以纯对象结束,就像OP当前的代码一样,这绝对是一个好方法。) - T.J. Crowder

1

好的,既然您正在使用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?
    }
}

根据您对我的问题评论的回复,最后一个应该是:
class SuccessResponse extends BaseErrorResponse {
    constructor(msg, rti, rsi, st, data) {
        super(msg, rti, rsi, st);
        this.data = data;
    }
}

0

对我来说,一个更简单的解决方案是:

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;
};

0

我使用了 T.J. 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);
})();

输出:

测试 )

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接