Last active
February 5, 2022 05:39
-
-
Save SidWorks/8d08cc128689e5ea36508791c183c211 to your computer and use it in GitHub Desktop.
Classless JavaScript- Use Modules Instead of Classes
This file contains hidden or 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
class Sentence{ | |
constructor(phrase){ | |
this.phrase = phrase | |
} | |
startCase(){ | |
this.phrase = this.phrase | |
.toLowerCase() | |
.split(' ') | |
.map(s => s.charAt(0).toUpperCase() + s.substring(1)) | |
.join(' ') | |
} | |
putQuotation(){ | |
this.phrase = `"${this.phrase}"` | |
} | |
get quote(){ | |
return this.phrase | |
} | |
} | |
const phrase = new Sentence("There is no secret ingredient, it’s just you") | |
phrase.startCase() | |
phrase.putQuotation() | |
const newName = phrase.quote | |
console.log(newName) | |
// "There Is No Secret Ingredient, It’s Just You" | |
class Sentence { | |
static startCase(phrase) { | |
return phrase | |
.toLowerCase() | |
.split(' ') | |
.map((s) => s.charAt(0).toUpperCase() + s.substring(1)) | |
.join(' ') | |
} | |
static putQuotation(phrase) { | |
return `"${phrase}"` | |
} | |
} | |
const startCase = Sentence.startCase("There is no secret ingredient, it’s just you") | |
const quote = Sentence.putQuotation(startCase) | |
console.log(quote) | |
// "There Is No Secret Ingredient, It’s Just You" | |
export function startCase(phrase){ | |
return phrase | |
.toLowerCase() | |
.split(' ') | |
.map((s) => s.charAt(0).toUpperCase() + s.substring(1)) | |
.join(' ') | |
} | |
export function getQuotation(phrase){ | |
return `"${phrase}"` | |
} | |
import {startCase, getQuotation as Sentence} from './sentence' | |
const phrase = Sentence.startCase('somnath singh') | |
const quote = Sentence.getQuotaion(phrase) | |
console.log(quote) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment