Skip to content

Instantly share code, notes, and snippets.

Created July 26, 2015 21:45
Show Gist options
  • Save anonymous/29183865d0d969515af0 to your computer and use it in GitHub Desktop.
Save anonymous/29183865d0d969515af0 to your computer and use it in GitHub Desktop.
Another Scrap from scrapfy.io
// 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