[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/Isco81/JavaScript-Universe/main/Objects_in_JavaScript [Back]  [Original]

_OBJECT__IN__Javascript_

Object is key value pairs where keys are the properties , typically stringa(or symboles), values can be any data type , functions or other object

Creating an Object:
1-> using normal or object literal
let person = {
	name:any_one,
	Age:22,
	isStudent : false,
	Greet : function(){
		console.log(hello my name is  + this.name);
	}
}
console.log(person);
person.Greet;

2-> using name object() Constructor
let  car = new Object();
car.brand = BMW,
car.model = DMZ1,
car.driver = function(){
	console.log(the car is driving)     
	}
console.log(car.brand);

Note: when an function is called inside an object then it is encapsulation as the user can't see what is inside that function without authentication

3=> using Object.create() - for prototype of inheritance
let  animal = {
	type:mammal,
	speak : function(){
		console.log(i am a  + this.type);
}
};
let dog = Object.create(animal);
dog.type = bear
dog.speak()


Accessing Object Properties

Dot notation
console.log(person.name); //xyz
Bracket Notation
console.log(person[name]); // XYZ


Modifying an object

1.Add a property
	Person.hobby = reading;
2.Update a property
	Person.age = 31;
3.delete a property
	Delete person.isStudent;

Object in functional Parameters

function introduce({name, age}){
console.log(`Hi , Im ${name} and my age is ${age}`);
}
Let person = { name : jane, age:23};
introduce(person)

Note: Object can be work in place of function to solve the examples of increment decrement and reset

Let counters = {
	Count:0,
	Increment: ()=>{
		this.count++;
		return count;
	},
	Decrement:()=>{
		this.count;
		return count;
	},
	Reset:()=>{
	This.count ==0;
	return count
},
Display : ()=>{
	console.log(current value  + this.count)
}
counters.Display();
console.log(counters.Increment());
console.log(counters.Increment())
console.log(counters.Decrement())
console.log(counters.Increment())
console.log(counters.Reset())
console.log(counters.Increment())
	

Q3-> Show how a deep copy of an object can be done
A deep copy creates a new object that is entirely independent of the original object, even for nested properties. 
1. Using structuredClone() (Modern & Recommended)
const Original = {name:apple, price:{perKg:240 Rupees}}
const DeepCopy = structuredClone(Original);

DeepCopy.price.perKg =  280 Rupees
console.log(Original.price.perKg)
console.log(DeepCopy.price.perKg)

Output: 240 Rupees
280 Rupees

2. Using JSON.parse(JSON.stringify())
This is a common method but has limitations, such as losing functions, undefined, Symbol, or circular references.

const Original = {name:apple, price:{perKg:240 Rupees}}
const DeepCopy =JSON.parse(JSON.stringify(Original));

DeepCopy.price.perKg =  280 Rupees
console.log(Original.price.perKg)
console.log(DeepCopy.price.perKg)

Output: 240 Rupees
280 Rupees


3. Using a Library (e.g., Lodash)
For complex objects or when structuredClone isn't available:
Import _ from lodash;
const Original = {name:apple, price:{perKg:240 Rupees}}
const DeepCopy =_.clone(Original);

DeepCopy.price.perKg =  280 Rupees
console.log(Original.price.perKg)
console.log(DeepCopy.price.perKg)

Output: 240 Rupees
280 Rupees



Web Proxy Viewer  |  New URL  |  Original Page