To create a JavaScript object that keeps track of a stock price, you can use the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
let stock = { symbol: "XYZ", // Symbol of the stock price: 100, // Current price of the stock change: function(newPrice) { this.price = newPrice; // Update the price of the stock }, display: function() { console.log("Stock: " + this.symbol + ", Price: " + this.price); } }; // Usage: stock.display(); // Output: Stock: XYZ, Price: 100 stock.change(110); stock.display(); // Output: Stock: XYZ, Price: 110 |
In this example, we created an object named stock
that has three properties: symbol
, price
, and two methods: change
and display
.
- The symbol property holds the symbol of the stock.
- The price property holds the current price of the stock.
- The change method is used to update the price of the stock. It takes a new price as a parameter and updates the price property.
- The display method logs the stock symbol and price to the console.
You can initialize the object with a starting symbol and price, and then use the change
method to update the price whenever it changes. The display
method can be used to log the current stock details.
What is a setter method in javascript objects?
In JavaScript, a setter method is a special function that is used to assign a value to a property of an object. It allows you to control how a property is updated or modified.
Setter methods follow a specific naming convention, starting with the keyword "set" followed by the capitalized property name you want to set. They take one parameter, representing the new value you want to assign to the property.
Here's an example of a setter method in a JavaScript object:
1 2 3 4 5 6 7 8 9 10 11 12 |
const person = { firstName: 'John', lastName: 'Doe', // Setter method for the 'firstName' property setFirstName: function(newFirstName) { this.firstName = newFirstName; } }; person.setFirstName('Jane'); console.log(person.firstName); // Output: 'Jane' |
In this example, the setFirstName
method takes a parameter newFirstName
and assigns it to the firstName
property of the person
object.
What is the concept of immutability in javascript objects?
In JavaScript, the concept of immutability refers to an object whose values cannot be changed after it is created. Once an immutable object is created, its properties and values remain constant throughout its lifetime, and any attempt to modify or update them will create a new object with the desired changes.
There are several ways to achieve immutability in JavaScript objects:
- Object.freeze(): The Object.freeze() method prevents any modification to the existing properties and values of an object. This includes adding, removing, or updating properties. Attempts to change the object will be ignored or throw an error in strict mode.
Example:
1 2 3 4 5 6 7 8 9 10 |
const person = { name: "John", age: 25 }; Object.freeze(person); person.name = "Jane"; // This change is ignored in strict mode. console.log(person); // Output: { name: "John", age: 25 } |
- Object.seal(): The Object.seal() method allows modifying existing properties of an object but prevents adding new properties or deleting existing ones. The values of sealed properties can still be changed.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
const person = { name: "John", age: 25 }; Object.seal(person); person.age = 30; // The value of the 'age' property can still be changed. console.log(person); // Output: { name: "John", age: 30 } person.gender = "Male"; // This change is ignored. delete person.name; // This change is ignored. console.log(person); // Output: { name: "John", age: 30 } |
- Immutable Libraries: There are various third-party libraries, such as Immutable.js, that provide data structures optimized for immutability. These libraries offer different methods and APIs to create immutable objects, collections, and data structures.
By ensuring immutability, JavaScript objects become more predictable and consistent, making it easier to reason about data flow, state management, and avoiding unintended side effects in code.
How to access object properties in javascript?
In JavaScript, you can access object properties using the dot notation or square bracket notation.
- Dot notation: To access an object property using dot notation, use the object name followed by a dot followed by the property name.
Example:
1 2 3 4 5 6 7 8 9 |
var person = { name: 'John', age: 30, city: 'New York' }; console.log(person.name); // Output: John console.log(person.age); // Output: 30 console.log(person.city); // Output: New York |
- Square bracket notation: To access an object property using square bracket notation, use the object name followed by square brackets containing the property name as a string.
Example:
1 2 3 4 5 6 7 8 9 |
var person = { name: 'John', age: 30, city: 'New York' }; console.log(person['name']); // Output: John console.log(person['age']); // Output: 30 console.log(person['city']); // Output: New York |
Square bracket notation is useful when property names are stored in variables or contain special characters. You can dynamically access properties using variables:
1 2 3 4 5 6 7 8 |
var person = { name: 'John', age: 30, city: 'New York' }; var propertyName = 'name'; console.log(person[propertyName]); // Output: John |
Note: The dot notation is more commonly used and easier to read, while the square bracket notation provides more flexibility.