Created
June 13, 2019 16:08
-
-
Save RajaJaganathan/d506c1138232b22a75075c64a70e1aec to your computer and use it in GitHub Desktop.
model_vs_helper.js - Favor for Model class than helper function because helper naming going to be redundant, expose internal structure(violate encapsulation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Model class approach | |
class UserModel { | |
constructor(data) { | |
this.data = data; | |
} | |
getSecondaryAddress() { | |
this.data.address.filter(address => address.secondary); | |
} | |
static async fetchUserBy(){ | |
// some api call | |
const result = {} | |
return new UserModel(result) | |
} | |
} | |
var user = await UserModel.fetchUserBy() | |
console.log(user.getSecondaryAddress()) | |
// Helper approach | |
//Helper.js | |
export async fetchUserBy(){ | |
const result = {} | |
return new UserModel(result) | |
} | |
export function getUserSecondaryAddress(address){ | |
return address.filter(address => address.secondary); | |
} | |
var user = await fetchUserBy() | |
console.log(getSecondaryAddress(user.address)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment