5, మార్చి 2025, బుధవారం

js classes & objects.............

 <!DOCTYPE html>

<html>

<body>

<h1>JavaScript Objects</h1>

<h2>Display Properties</h2>


<p id="demo"></p>


<script>

// Create an Object

const person = {

  name: "RAMU",

  age: 40,

  city: "HYDERABAD, loc AMERPET"

};


// Build a Text

let text = "";

for (let x in person) {

  text += person[x] + "          ";

};


// Display the Text

document.getElementById("demo").innerHTML = text;


</script>


</body>

</html>

<!DOCTYPE html>

<html>

<body>

<h1>js class objects functions </h1>

<h2>Adding a Property to a Constructor</h2>


<p id="demo"></p>


<script>

// Constructor function for Person Objects

function Person(first, last, age, eye) {

  this.f = first;

  this.l = last;

  this.age = age;

  this.eyecol = eye;

}


// Create 2 Person Objects

const myFather = new Person("ramu", "dayina", 40, "blue");

const myMother = new Person("ravi", "verma", 48, "green");


// Add a new Property

Person.prototype.nationality = "indian";


// Display new Property

document.getElementById("demo").innerHTML =

"The nationality of my father is "

 + myFather.nationality+ "<br>"

+"display from the FATHER object "+ "<br>"+myFather.f+  "<br>"+myFather.l+ "<br>"+myFather.age

+ "<br>"+myFather.eyecol+"<br>"

+"display from the MOTHER object "

+ "<br>"+myMother.f+ "<br>"

+myMother.l+ "<br>"+myMother.age+ "<br>"

+myMother.eyecol; 


</script>


</body>

</html>





కామెంట్‌లు లేవు:

కామెంట్‌ను పోస్ట్ చేయండి