Example : A JS program to create an object through various ways, store property value, and finally display the output.
(Object Creation through Object Literal method)

<html>
   <head>			
	<script>			
		var bk1 = new Object(); // Creation of the object name bk1
		bk1.book_name = "Computer Fundamentals"; //Assign property value to the object
		bk1.author = "TMHI";
	</script>			
   </head>
	
	<body>
		<script>		
			document.write("Book name is : " + bk1.book_name + "<br>");
			document.write("Book author is : " + bk1.author + "<br>");
		</script>
	</body>
</html>

Output :
Book name is : Computer Fundamentals
Book author is : TMHI

-----------------  OR  -------------------

(Object Creation through Object Literal method)

<html>
	<head></head>	
	<body>
		<script>				
			const person = {
				name: "Robert",
				age: 24,
				location: "USA",
				display() 
                                {
					document.write(`Hi, My name is ${this.name}, I'm ${this.age} years old and Live in ${this.location}.`);
				}
			};
			person.display();  
		</script>	
	</body>
</html>

Output:
Hi, My name is Robert, I'm 24 years old and Live in USA.

NB:-In this example, we define an object called person with three properties (name, age and location) and a display method using the object literal syntax.

--------------  OR  --------------

(Object Creation through Constructor function method)

<html>
	<head></head>	
	<body>
		<script>					
			function Person(name, age) {
				this.name = name;
				this.age = age;
				this.display = function() 
				{
				  document.write(`Hi, My name is ${this.name} and I'm ${this.age} years old.`);
				};
			}

			const obj1 = new Person("Robert", 19);
			obj1.display();				  
		</script>	
	</body>
</html>

Output:
Hi, My name is Robert and I'm 19 years old

NB:
In this example, we define a constructor function called Person that takes two parameters (name and age). Inside the constructor, we set the name and age properties of the object being created. We also define a display method using a function expression. To create an instance of the Person object, we use the 'new' keyword followed by the constructor name and the constructor arguments. We then call the display method on the new object to output the message to the console.

------------------  OR  -------------------

(Object Creation through Object.create() method)

<html>
	<head></head>	
	<body>
		<script>					
			const person = {
			     display() {
					document.write(`Hi, My name is ${this.name} and I'm ${this.age} years old.`);
				}
			};

			const obj1 = Object.create(person);
			obj1.name = "Robert";
			obj1.age = 22;

			obj1.display();				  
		</script>	
	</body>
</html>

Output:
Hi, My name is Robert and I'm 22 years old.

NB:In this example, we define an object called person with a display method. We then create a new object called obj1 using the Object.create() method and passing in person as the prototype. We then set the name and age properties of the obj1 object. Finally, we call the display method on the obj1 object to output the message to the console.
Example: A JS program to create a class & object, store property value, and finally display the output.
<html>
	<head></head>	
	<body>
		<script>			
				
			class Car 
			{
				constructor(make, model, year) 
				{
					this.make = make;
					this.model = model;
					this.year = year;
				}  
				
				display() 
				{
					document.write(`The Car company, model and manufacturing year is =  ${this.make} ${this.model} and ${this.year}.`);
				}
			}
			
			//const Obj1 = new Car('Honda', 'Civic', 2021);
			var Obj1 = new Car('Honda', 'Civic', 2023);
			Obj1.display(); 
		</script>	
	</body>
</html>

Output:
The Car company, model and manufacturing year is = Honda Shine and 2023.

Loading


0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.