2016-02-13 155 views
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": {} 
    } 
}; 
+0

在'SuccessResponse',没你的意思' “数据”:数据'而不是''数据“:{}'?另外,为什么'null's? –

+0

哦......我错过了!它只是一项正在进行的工作。我仍在完成...... – dearvivekkumar

+0

MDN [继承和原型链](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Inheritance_and_the_prototype_chain) – Roberto

回答

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

你可以只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": {} 
    }); 
}; 

这可能是一个好主意,让这些成为现实然而,相互继承的构造函数。

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的代码目前是这样做的,所以很有用/有用。) –

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.如下克罗德代码及其对我

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