Home » Tech Tutorials » Web Development » JavaScript » JavaScript Object Properties and Methods

JavaScript Object Properties and Methods

JavaScript Object Properties and Methods

JavaScript object లోని data ను properties అని, object చేసే actions ను methods అని అంటారు.

Object Properties

Property అనేది object లోని ఒక named value. ఉదాహరణకు name, age, price వంటివి properties కావచ్చు.

Property value ను dot notation తో object.property లేదా bracket notation తో object[“property”] access చేయవచ్చు.

కొత్త property add చేయడానికి లేదా ఉన్న property value మార్చడానికి assignment operator ఉపయోగించవచ్చు. Property తొలగించడానికి delete keyword ఉపయోగించవచ్చు.

Object Methods

Object లో function ను property గా ఉంచినప్పుడు దాన్ని method అంటారు. Method ను call చేయడానికి object.method() syntax ఉపయోగిస్తాం.

Object method లోని this keyword సాధారణంగా ఆ method ను call చేసిన object ను సూచిస్తుంది. అందువల్ల this.name ద్వారా అదే object లోని name property ను access చేయవచ్చు.

Properties మరియు Methods మధ్య తేడా

  • Property: Object లో data ను store చేస్తుంది.
  • Method: Object పై action లేదా behavior ను define చేస్తుంది.

ఉదాహరణకు ఒక bankAccount object లో balance ఒక property, deposit() ఒక method.

Simple Explanation

Object ను ఒక real-world entity గా ఊహించండి. ఒక mobile phone object కు brand, color, price వంటి properties ఉంటాయి. అదే phone లో call(), playMusic() వంటి methods ఉంటాయి.

అంటే properties object గురించి information ను ఉంచుతాయి; methods object చేయగల పనులను define చేస్తాయి.

Real-Life Example

ఒక shopping cart ను object గా తీసుకుంటే, items మరియు totalPrice దాని properties. addItem(), removeItem(), calculateTotal() దాని methods. Properties cart లోని current data ను చూపిస్తాయి, methods ఆ data పై operations చేస్తాయి.

Code Examples

Object Properties ను Access, Update మరియు Delete చేయడం

const student = {
  name: "Ravi",
  age: 20,
  course: "JavaScript"
};

console.log(student.name);
student.age = 21;
student.city = "Hyderabad";
delete student.course;

console.log(student.age);
console.log(student.city);
console.log(student.course);

ఈ example లో student object లోని properties ను access చేస్తున్నాం. student.age ద్వారా age update అవుతుంది, student.city ద్వారా కొత్త property add అవుతుంది. delete student.course ద్వారా course property తొలగించబడుతుంది. తొలగించిన property ను access చేస్తే undefined వస్తుంది.

Ravi
21
Hyderabad
undefined

Object Method మరియు this Keyword

const user = {
  name: "Anu",
  greet: function () {
    return "Hello, " + this.name;
  }
};

console.log(user.greet());

ఇక్కడ greet ఒక object method. Method లోని this.name అనేది user object లోని name property ను సూచిస్తుంది. user.greet() call చేసినప్పుడు greeting message return అవుతుంది.

Hello, Anu

Method ద్వారా Property Value మార్చడం

const account = {
  balance: 1000,
  deposit: function (amount) {
    this.balance = this.balance + amount;
  }
};

account.deposit(500);
console.log(account.balance);

ఈ account object లో balance ఒక property, deposit ఒక method. deposit method కు amount పంపినప్పుడు this.balance లో ఆ amount కలుస్తుంది. Method object యొక్క property value ను మార్చగలదని ఇది చూపిస్తుంది.

1500

Line-by-Line Explanation

const user = {

user అనే constant లో ఒక కొత్త object ను create చేస్తున్నాం.

name: “Anu”,

Object లో name అనే property కి “Anu” value ఇస్తున్నాం.

greet: function () {

greet అనే property లో function ను ఉంచుతున్నాం. Function property కావడం వల్ల ఇది object method అవుతుంది.

return “Hello, ” + this.name;

ఈ line greeting string ను return చేస్తుంది. this.name ద్వారా user object లోని name value పొందుతుంది.

}

greet method యొక్క function body ఇక్కడ ముగుస్తుంది.

};

user object definition ఇక్కడ ముగుస్తుంది.

console.log(user.greet());

user object లోని greet method ను call చేసి, return అయిన message ను console లో print చేస్తున్నాం.

Try It

Try this JavaScript code:

Example

const product = {
  name: "Notebook",
  price: 50,
  stock: 10
};

console.log(product.name);
console.log(product.stock);

// Add an increaseStock method here.
// The method should receive an amount and add it to stock.

// Call the method with 5 and print the updated stock.

Try It Yourself »

Practice

Practice:

Practice Task: product object కు increaseStock అనే method ను add చేయండి. అది amount అనే parameter తీసుకొని stock property కి ఆ amount ను కలపాలి. తరువాత method ను 5 argument తో call చేసి updated stock ను print చేయండి.

Hint:

Method లో this.stock = this.stock + amount వంటి statement ఉపయోగించండి. తరువాత product.increaseStock(5) ను call చేయండి.

Common Mistakes

Property ను access చేసేటప్పుడు student[age] అని quotes లేకుండా రాయడం.

Bracket notation లో property name string అయితే student[“age”] అని quotes ఉపయోగించాలి. Variable ఉపయోగిస్తే మాత్రమే quotes అవసరం ఉండకపోవచ్చు.

Method లో object property కోసం this బదులు తప్పు variable name ఉపయోగించడం.

Object method లో current object property ను access చేయడానికి సాధారణంగా this.propertyName ఉపయోగించండి.

Method ను call చేయకుండా user.greet అని రాయడం.

Function ను execute చేయడానికి parentheses అవసరం. కాబట్టి user.greet() అని call చేయాలి.

Object property పేరు మరియు method పేరు ఒకటే అని భావించడం.

Property data ను store చేస్తుంది; method function ను store చేసి action ను perform చేస్తుంది.

Interview Questions

JavaScript object property అంటే ఏమిటి?

Answer:

Object లో named value ను property అంటారు. ఉదాహరణకు name: “Ravi” లో name ఒక property.

Object method అంటే ఏమిటి?

Answer:

Object లో function గా ఉన్న property ను method అంటారు. ఉదాహరణకు greet: function () { } ఒక method.

Dot notation మరియు bracket notation మధ్య తేడా ఏమిటి?

Answer:

Dot notation లో object.property అని రాస్తాం. Bracket notation లో object[“property”] అని రాస్తాం. Property name dynamic గా ఉన్నప్పుడు bracket notation ఉపయోగకరంగా ఉంటుంది.

Object method లో this keyword ఏమి సూచిస్తుంది?

Answer:

సాధారణ method call సందర్భంలో this ఆ method ను call చేసిన object ను సూచిస్తుంది.

Exam Questions

ఈ code output ఏమిటి? const car = { brand: “Toyota”, start: function () { return this.brand + ” started”; } }; console.log(car.start());

Answer:

Output: Toyota started. ఎందుకంటే this.brand, car object లోని brand property ను సూచిస్తుంది.

Object లో కొత్త property add చేయడానికి ఏ statement ఉపయోగించవచ్చు?

Answer:

object.newProperty = value; అనే assignment ఉపయోగించవచ్చు. ఉదాహరణకు student.city = “Hyderabad”;.

ఒక object property తొలగించడానికి ఏ keyword ఉపయోగిస్తారు?

Answer:

delete keyword ఉపయోగిస్తారు. ఉదాహరణకు delete student.course;.

Quick Revision

  • Object లోని data values ను properties అంటారు.

  • Object లో function గా ఉన్న property ను method అంటారు.

  • Property access కోసం dot notation లేదా bracket notation ఉపయోగించవచ్చు.

  • కొత్త property add చేయడానికి assignment ఉపయోగించండి.

  • Property value మార్చడానికి అదే property కి కొత్త value assign చేయండి.

  • Property తొలగించడానికి delete keyword ఉపయోగించండి.

  • Object method లో this సాధారణంగా current object ను సూచిస్తుంది.

  • Method ను execute చేయడానికి parentheses తో call చేయాలి.

Test Your Knowledge

Take a short quiz based on this lesson.

10 Questions • Results
Scroll to Top