xxxxxxxxxx
# Define a class
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def greet(self):
print(f"Hello, my name is {self.name} and I'm {self.age} years old.")
# Create an object (instance) of the Person class
person = Person("John", 25)
# Access object properties
print(person.name) # Output: John
print(person.age) # Output: 25
# Call object methods
person.greet() # Output: Hello, my name is John and I'm 25 years old.
xxxxxxxxxx
The Object is the real-time entity having some state and behavior.
In Java, Object is an instance of the class having the instance variables
as the state of the object and the methods as the behavior of the object.
The object of a class can be created by using thenewkeyword
xxxxxxxxxx
[object Object]
/*
This means that your programming interpreter doesn't support showing
JSON parsers somewhere, and has resorted to the default object representation
behavior (see below).
Behavior: [object CLASS]
Since it's an object, well: [object Object]
This is most commonly seen in a depraved, deranged state in objects
ingrained into text. We still haven't really devised a prophecy
about why it still happens in some places. Either JavaScript likes to
be vexing by being "open" aka adding unnecessary steps, or JavaScript
has something like security issues related to its code/some other
predicament retaining and pertaining.
*/
xxxxxxxxxx
function namesOnly(arr){
// your code here
}
console.log(namesOnly([
{
name: "Angelina Jolie",
age: 80
},
{
name: "Eric Jones",
age: 2
},
{
name: "Paris Hilton",
age: 5
},
{
name: "Kayne West",
age: 16
},
{
name: "Bob Ziroll",
age: 100
}
]));
// ["Angelina Jolie", "Eric Jones", "Paris Hilton", "Kayne West", "Bob Ziroll"]
xxxxxxxxxx
// JavaScript objects are a fundamental data structure used to represent various entities.
// They have properties that can be used to store key-value pairs of data.
// Create an object using object literal notation
const person = {
name: "John",
age: 25,
city: "New York"
};
// Accessing object properties
console.log(person.name); // Output: "John"
console.log(person.age); // Output: 25
// Adding a new property to the object
person.gender = "Male";
// Updating an existing property
person.age = 26;
// Deleting a property
delete person.city;
// Looping through object properties
for (let key in person) {
console.log(key + ": " + person[key]);
}
// Output:
// name: John
// age: 26
// gender: Male
xxxxxxxxxx
{
"key": "ctrl+shift+r ctrl+e",
"command": "editor.action.codeAction",
"args": {
"kind": "refactor.extract.function"
}
}
xxxxxxxxxx
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def introduce(self):
print(f"My name is {self.name} and I am {self.age} years old.")
# Creating an object/instance of the Person class
person1 = Person("John", 25)
# Accessing object attributes and invoking methods
print(person1.name) # Output: John
print(person1.age) # Output: 25
person1.introduce() # Output: My name is John and I am 25 years old.