You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Q4. What is the difference between dot notation and square bracket notation in accessing object properties?
Answer: The dot notation is used when you know the property name at the time of coding. The square bracket notation is used when you want to access the property dynamically, for example, when the property name is stored in a variable.
Q5. How do you add a new property to an object in JavaScript?
Answer: You can add a new property to an object using the dot notation or the square bracket notation as follows:
person.city='Chennai'// Using dot notationperson['country']='India'// Using square bracket notation
Q6. How do you remove a property from an object in JavaScript?
Answer: You can remove a property from an object using the delete keyword as follows:
deleteperson.city
Q7. How do you check if an object has a property in JavaScript?
Answer: You can check if an object has a property using the hasOwnProperty method as follows:
Q11. What is the difference between an object created using object literal notation and an object created using a constructor function?
Answer: An object created using object literal notation is a single instance, while an object created using a constructor function can create multiple instances.
Q12. What is prototypal inheritance in JavaScript?
Answer: Prototypal inheritance is a way of creating new objects based on an existing object. The new object inherits the properties and methods of the existing object.
Q13. How do you create an object using prototypal inheritance in JavaScript?
Answer: You can create an object using prototypal inheritance as follows:
letperson={name: 'Anbu',age: 27,greet: function(){console.log(`Hello, my name is ${this.name} and I am ${this.age} years old`)},}letemployee=Object.create(person)employee.jobTitle='Software Developer'
Q14. What is the difference between an instance property and a prototype property in JavaScript?
Answer: An instance property is a property that is specific to a particular instance of an object. When you create a new instance of an object, that instance will have its own copy of the instance properties. Any changes made to the instance property of one object do not affect the instance properties of other objects.
On the other hand, a prototype property is a property that is shared among all instances of an object. When you create an object, you can add properties to its prototype that will be shared by all instances of the object. If you change the value of a prototype property, that change will be reflected in all instances of the object.
In this example, name and age are instance properties because they are specific to each instance of the Person object. gender is a prototype property because it is shared by all instances of the Person object. When we change the value of the gender property on the prototype, the change is reflected in all instances of the Person object.
Q15. What is the output of the following code?
letperson={name: 'Anbu',age: 27,greet: function(){console.log('Hello, my name is '+this.name+' and I am '+this.age+' years old.')},}person.greet()
Output: Hello, my name is Anbu and I am 27 years old.
Q16. What is the output of the following code?
letperson={name: 'Anbu',age: 27,address: {street: 'Dubai Main Road',city: 'Dubai',state: 'UAE',},}console.log(person.address.city)
letperson={name: 'Anbu',age: 27,greet: function(){console.log('Hello, my name is '+this.name+' and I am '+this.age+' years old.')},}letgreetFunc=person.greetgreetFunc()
Output: Hello, my name is undefined and I am undefined years old.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Below are the questions:
Q1. What is an object in JavaScript?
Q2. How do you create an object in JavaScript?
Q3. How do you access a property of an object in JavaScript?
Q4. What is the difference between dot notation and square bracket notation in accessing object properties?
Q5. How do you add a new property to an object in JavaScript?
Q6. How do you remove a property from an object in JavaScript?
Q7. How do you check if an object has a property in JavaScript?
Q8. How do you loop through the properties of an object in JavaScript?
Q9. What is a constructor function in JavaScript?
Q10. How do you create an object using a constructor function in JavaScript?
Q11. What is the difference between an object created using object literal notation and an object created using a constructor function?
Q12. What is prototypal inheritance in JavaScript?
Q13. How do you create an object using prototypal inheritance in JavaScript?
Q14. What is the difference between an instance property and a prototype property in JavaScript?
Q15. What is the output of the following code?
Q16. What is the output of the following code?
Q17. What is the output of the following code?
Q18. What is the output of the following code?
Q19. What is the output of the following code?
Q20. What is the output of the following code?
Q21. What is the output of the following code?
Q22. What is the output of the following code?
Q23. What is the output of the following code?
Q24. What is the output of the following code?
Q25. What is the output of the following code?
Beta Was this translation helpful? Give feedback.
All reactions