Questions tagged [object]

An object is any entity that can be manipulated by commands in a programming language. An object can be a value, a variable, a function, or a complex data-structure. In object-oriented programming, an object refers to an instance of a class.

Objects in object-oriented programming (OOP) are data structures combined with the associated processing routines. Classes are sets of objects, while objects are instances of sets. Objects have members and methods, defining their properties and their abilities. Classes can have their own members and methods, which define the properties and abilities of the set of objects. For instance, if we have a Bird class, its objects might have an age property and a fly ability, while the Bird class might have a number of birds or a migrate ability, which is applicable for the set. Class-level methods are called static, or shared. For instance, a file could be represented as an object: a collection of data and the associated read and write routines. In typical object-oriented languages, all objects are instances of classes.

Properties of an object

Three properties characterize objects:

  • Identity: the property of an object that distinguishes it from other objects
  • State: describes the data stored in the object
  • Behavior: describes the methods in the object's interface by which the object can be used

See also:

  • (Used as a template for creating new objects)
  • (Object-oriented programming)

Resource

64898 questions
7395
votes
38 answers

How do I remove a property from a JavaScript object?

Given an object: let myObject = { "ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*" }; How do I remove the property regex to end up with the following myObject? let myObject = { "ircEvent": "PRIVMSG", "method": "newURI" };
johnstok
  • 96,212
  • 12
  • 54
  • 76
5169
votes
67 answers

What is the most efficient way to deep clone an object in JavaScript?

What is the most efficient way to clone a JavaScript object? I've seen obj = eval(uneval(o)); being used, but that's non-standard and only supported by Firefox. I've done things like obj = JSON.parse(JSON.stringify(o)); but question the efficiency. …
jschrab
  • 11,035
  • 4
  • 20
  • 17
3976
votes
33 answers

Checking if a key exists in a JavaScript object?

How do I check if a particular key exists in a JavaScript object or array? If a key doesn't exist, and I try to access it, will it return false? Or throw an error?
Adam Ernst
  • 52,440
  • 18
  • 59
  • 71
3201
votes
49 answers

Detecting an undefined object property

How do I check if an object property in JavaScript is undefined?
Matt Sheppard
  • 116,545
  • 46
  • 111
  • 131
2995
votes
44 answers

Length of a JavaScript object

I have a JavaScript object. Is there a built-in or accepted best practice way to get the length of this object? const myObject = new Object(); myObject["firstname"] = "Gareth"; myObject["lastname"] = "Simpson"; myObject["age"] = 21;
Gareth Simpson
  • 36,943
  • 12
  • 47
  • 50
2385
votes
17 answers

Calling a function of a module by using its name (a string)

How do I call a function, using a string with the function's name? For example: import foo func_name = "bar" call(foo, func_name) # calls foo.bar()
ricree
  • 35,626
  • 13
  • 36
  • 27
2383
votes
16 answers

How to check if an object has an attribute?

How do I check if an object has some attribute? For example: >>> a = SomeClass() >>> a.property Traceback (most recent call last): File "", line 1, in AttributeError: SomeClass instance has no attribute 'property' How do I tell if…
Lucas Gabriel Sánchez
  • 40,116
  • 20
  • 56
  • 83
2340
votes
32 answers

Iterate through object properties

var obj = { name: "Simon", age: "20", clothing: { style: "simple", hipster: false } } for(var propt in obj){ console.log(propt + ': ' + obj[propt]); } How does the variable propt represent the…
Rafay
  • 23,785
  • 4
  • 20
  • 27
1901
votes
2 answers

How do I check if an object has a key in JavaScript?

Which is the right thing to do? if (myObj['key'] == undefined) or if (myObj['key'] == null) or if (myObj['key'])
kevin
1656
votes
6 answers

Why do Python classes inherit object?

Why does the following class declaration inherit from object? class MyClass(object): ...
tjvr
  • 17,431
  • 6
  • 25
  • 26
1458
votes
9 answers

Null object in Python

How do I refer to the null object in Python?
Lizard
  • 43,732
  • 39
  • 106
  • 167
1458
votes
10 answers

Object comparison in JavaScript

What is the best way to compare objects in JavaScript? Example: var user1 = {name : "nerd", org: "dev"}; var user2 = {name : "nerd", org: "dev"}; var eq = user1 == user2; alert(eq); // gives false I know that two objects are equal if they refer to…
spankmaster79
  • 21,555
  • 10
  • 42
  • 73
1372
votes
23 answers

Convert JS object to JSON string

If I defined an object in JS with: var j={"name":"binchen"}; How can I convert the object to JSON? The output string should be: '{"name":"binchen"}'
Bin Chen
  • 61,507
  • 53
  • 142
  • 183
1192
votes
42 answers

Converting an object to a string

How can I convert a JavaScript object into a string? Example: var o = {a:1, b:2} console.log(o) console.log('Item: ' + o) Output: Object { a=1, b=2} // very nice readable output :) Item: [object Object] // no idea what's inside :(
user680174
  • 11,921
  • 3
  • 15
  • 3
1163
votes
20 answers

Difference between ( for... in ) and ( for... of ) statements?

I know what is a for... in loop (it iterates over the keys), but I have heard about for... of for the first time (it iterates over values). I am confused about for... of loop. var arr = [3, 5, 7]; arr.foo = "hello"; for (var i in arr) { …
Mukund Kumar
  • 21,413
  • 18
  • 59
  • 79
1
2 3
99 100