2016-02-13 154 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' } }