The new Object()
syntax creates an empty object using the Object
constructor. You can then add properties and methods to this object dynamically. While less common than object literals, it can be useful in certain programmatic scenarios.
Example 1: Building an Object Step-by-Step
// Create an empty object using the Object constructor.
const book = new Object();
// Add properties to the object.
book.title = "The Great Gatsby";
book.author = "F. Scott Fitzgerald";
book.pages = 180;
// Log the title property.
console.log(book.title); // Outputs: The Great Gatsby
Explanation
This code first creates an empty object called book
with new Object()
. It then assigns properties (title
, author
, pages
) to this object one by one.
Example 2: Adding a Method after Creation
// Create an empty pet object.
const pet = new Object();
// Assign properties.
pet.name = "Buddy";
pet.type = "Dog";
// Assign a method.
pet.speak = function() {
console.log("Woof!");
};
// Call the method.
pet.speak(); // Outputs: Woof!
Explanation
Similar to the first example, we create an empty pet
object. We then add both data properties (name
, type
) and a functional property (a method called speak
).
Example 3: Conditional Property Addition
// Create a base recipe object.
const recipe = new Object();
recipe.name = "Pancakes";
const hasSyrup = true;
// Add a property based on a condition.
if (hasSyrup) {
recipe.topping = "Maple Syrup";
}
console.log(recipe.topping); // Outputs: Maple Syrup
Explanation
This example shows the flexibility of new Object()
. An empty recipe
object is created, and then a topping
property is added only if the hasSyrup
condition is true.
Example 4: Using Bracket Notation for Properties
// Create a new object for storing scores.
const gameScores = new Object();
// Use bracket notation to add properties, useful for names with spaces.
gameScores["Player 1"] = 1500;
gameScores["Player 2"] = 1250;
console.log(gameScores["Player 1"]); // Outputs: 1500
Explanation
This code uses bracket notation []
instead of dot notation to add properties. Bracket notation is required when property names are not valid JavaScript identifiers, such as those containing spaces.
Example 5: Object from User Input
// Simulate creating an object from user form input.
const formInput = new Object();
formInput.username = "newuser"; // Imagine this comes from an input field
formInput.password = "pass123"; // And this from another
console.log(formInput.username); // Outputs: newuser
Explanation
This illustrates how you might collect data into an object. An empty formInput
object is instantiated and then populated, simulating how you might handle data from a web form.
Example 6: A Simple Counter
// Create a counter object.
const counter = new Object();
counter.value = 0;
// Add a method to increment the value.
counter.increment = function() {
this.value++;
};
counter.increment();
console.log(counter.value); // Outputs: 1
Explanation
An object counter
is created with an initial value
. A method increment
is then added to modify the object's own value
property using this
.
Example 7: Storing File Metadata
// Create an object to hold file metadata.
const fileMeta = new Object();
// Populate the object with file details.
fileMeta.fileName = "document.pdf";
fileMeta.fileSize = "2.5MB";
fileMeta.mimeType = "application/pdf";
console.log(`File: ${fileMeta.fileName}`); // Outputs: File: document.pdf
Explanation
This demonstrates a practical use case for grouping related data. The fileMeta
object neatly contains all the relevant metadata for a single file.