Ways to Create Objects
Posted on September 16, 2020 in JavaScript by Matt Jennings
- Object literal
- Dot notation
- Object.create
// Create an empty object const user3 = Object.create(null); // const user3 = {};
- Generate an Object with a Function (not a good idea!)
function userCreator(name, score) { const newUser = {}; newUser.name = name; newUser.score = score; newUser.increment = function() { newUser.score++; } return newUser; } const user1 = userCreator("Phil", 4); user1.increment(); console.log(user1.score); // 5