Skip to content

Instantly share code, notes, and snippets.

@blackakula
Last active April 18, 2024 15:34
Show Gist options
  • Select an option

  • Save blackakula/e2bf38bc7d1c540700b7e0c6c26f6c85 to your computer and use it in GitHub Desktop.

Select an option

Save blackakula/e2bf38bc7d1c540700b7e0c6c26f6c85 to your computer and use it in GitHub Desktop.
Classes versus co-data
class ClassWhoAmI {
#prefix = 'It is the secret'
suffix = 'person'
static additional = 'bad one'
constructor(parameter) {
this.parameter = parameter
}
getValue() {
return `${this.#prefix}: ${this.parameter} ${this.suffix}`
}
}
var testClassObject = new ClassWhoAmI("I'm")
testClassObject.suffix = 'programmer'
console.log(testClassObject.getValue(), '-', ClassWhoAmI.additional)
var DataWhoAmI = Object.assign((parameter) => {
var prefix = 'It is the secret'
var coData = {
suffix: 'person',
getValue: () => `${prefix}: ${parameter} ${coData.suffix}`
}
return coData
}, {additional: 'good one'})
var testDataObject = DataWhoAmI("I'm")
testDataObject.suffix = 'programmer'
console.log(testDataObject.getValue(), '-', DataWhoAmI.additional)
function FuncWhoAmI(parameter) {
this.suffix = 'person'
this.parameter = parameter
}
FuncWhoAmI.additional = 'old one'
FuncWhoAmI.prototype.getValue = function () {
return `It can not be a secret: ${this.parameter} ${this.suffix}`
}
var testFuncObject = new FuncWhoAmI("I'm")
testFuncObject.suffix = 'programmer'
console.log(testFuncObject.getValue(), '-', FuncWhoAmI.additional)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment