Created
July 26, 2015 21:45
-
-
Save anonymous/29183865d0d969515af0 to your computer and use it in GitHub Desktop.
Another Scrap from scrapfy.io
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
// Dynamic classnames | |
// ================== | |
// Question | |
// ================== | |
// Basic use | |
class User{ | |
// ... | |
} | |
let item = new User(); | |
// I want something like... | |
let classname = 'User'; | |
let item = new {classname}(); | |
// Solutions | |
// ================== | |
// Dictionnnary | |
const myDictionnaryOfModels = new Map(); | |
//… | |
myDictionnaryOfModels.set(‘User’, User); // or User.name, User | |
//… | |
myDictionnaryOfModels.get(classname); | |
// factory pattern | |
function createModel(modelname) { | |
return new require('../models/' + modelname)(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment