Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

JavaScript Object,Property,Methods

The document provides an overview of JavaScript objects, including their properties and methods. It explains how to create objects, access their properties, and utilize various methods such as defineProperty, getOwnPropertyNames, assign, and values. Code examples illustrate the concepts discussed, demonstrating object creation and manipulation in JavaScript.

Uploaded by

ROSELIN ANDREW
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

JavaScript Object,Property,Methods

The document provides an overview of JavaScript objects, including their properties and methods. It explains how to create objects, access their properties, and utilize various methods such as defineProperty, getOwnPropertyNames, assign, and values. Code examples illustrate the concepts discussed, demonstrating object creation and manipulation in JavaScript.

Uploaded by

ROSELIN ANDREW
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

JavaScript

Object,Property,Methods

By: H.L. Chandrakar

03/01/2025 Javascript By H.L. Chandrakar


What is JavaScript Object
• A JavaScript object is basically a collection of
unordered properties.
• Values associated with a JavaScript object are
called its properties.
• Actions that can be performed on a JavaScript
object are called methods.

03/01/2025 Javascript By H.L. Chandrakar


<script>
var student = { name: “Rahul", age: 21};
</script>
JavaScript Properties

name age
student Rahul 21

JavaScript Object
Property values

03/01/2025 Javascript By H.L. Chandrakar


Way 1:Object Creation
• <script>
• var student = { name: “Rahul", age: 21,
branch: "Computer Science", };
• </script>

03/01/2025 Javascript By H.L. Chandrakar


Way 2:Object Creation
• <script>
• var student = new Object(); student.name =
“Rahul"; student.age = 21; student.branch =
"Computer Science";
• </script>

03/01/2025 Javascript By H.L. Chandrakar


Accessing Object Properties
• The first method is to access the property by
using the dot(.) notation - object.property
• Second, we can store a property name, in the
form of a string, in a variable and then use
that variable to access the associated
property.

03/01/2025 Javascript By H.L. Chandrakar


<html>
<body>
<script>

const student={name:"Sunil",age:30};

alert("Student name="+student.name);
alert("Student age="+student.age);

var x=student.name;
document.write(x);

</script>
</body>
</html>
03/01/2025 Javascript By H.L. Chandrakar
JavaScript Methods
1)defineProperty(): this method is used to add
new property and its value into object or to
change property value of any object

03/01/2025 Javascript By H.L. Chandrakar


<html>
<body>

<script>
// Create an Object:
const person = {
firstName: "John",
lastName: "Doe",
language: "EN"
};

// Add a Property
Object.defineProperty(person, "year", {value:"2008"})

// Display the Property


document.write(person.year);
</script>

</body>
</html>

03/01/2025 Javascript By H.L. Chandrakar


<html>
<body>
<h1>JavaScript Object</h1>
<h2>The defineProperty() Method</h2>

<p>This example changes a property value.</p>

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

<script>
// Create an Object:
const person = {
firstName: "John",
lastName: "Doe",
language: "EN"
};

// Change a Property
Object.defineProperty(person, "language", {value:"NO"})

// Display the Property


document.write( person.language);
</script>

</body>
</html>

03/01/2025 Javascript By H.L. Chandrakar


JavaScript Methods
• 2) getOwnPropertyNames():it is used to
display all the properties of any object.

03/01/2025 Javascript By H.L. Chandrakar


<html>
<body>
<h1>JavaScript Object</h1>
<h2>The getOwnPropertyNames() Method</h2>

<p>This example list all the properties of an object.</p>

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

<script>
// Create an Object:
const person = {
firstName: "John",
lastName: "Doe",
language: "EN"
}

// Display all Properties


document.write( Object.getOwnPropertyNames(person));
</script>

</body>
</html>
03/01/2025 Javascript By H.L. Chandrakar
JavaScript Methods
3)assign() method: Copies properties from a
source object to a target object
Object.assign(target, source)

03/01/2025 Javascript By H.L. Chandrakar


// Create Target Object
const person1 = {
firstName: "John",
lastName: "Doe",
age: 50,
eyeColor: "blue"
};

// Create Source Object


const person2 = {firstName: "Anne",lastName: "Smith"};

// Assign Source to Target


Object.assign(person1, person2);

03/01/2025 Javascript By H.L. Chandrakar


JavaScript Methods
4)values() method: returns a single dimension
array of the object values

03/01/2025 Javascript By H.L. Chandrakar


<html>
<body>
<h1>JavaScript Objects</h1>
<h2>The Object.values() Method</h2>
<p>Object.values() returns an array of values from an object:</p>

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

<script>
const person = {
firstName : "John",
lastName : "Doe",
age : 50,
eyeColor : "blue"
};

let text = Object.values(person)


document.write(text);
</script>

</body>
</html>

03/01/2025 Javascript By H.L. Chandrakar


OUTPUT

JavaScript Objects
The Object.values() Method
Object.values() returns an array of values from
an object:
John,Doe,50,blue

03/01/2025 Javascript By H.L. Chandrakar

You might also like