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

56770 questions
5175
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
  • 18
  • 17
3370
votes
25 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
  • 45,666
  • 17
  • 55
  • 71
3003
votes
48 answers

Detecting an undefined object property

What's the best way of checking if an object property in JavaScript is undefined?
Matt Sheppard
  • 111,039
  • 46
  • 105
  • 128
2182
votes
30 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
  • 22,175
  • 4
  • 18
  • 27
1989
votes
14 answers

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

What is the best way to go about calling a function given a string with the function's name in a Python program. For example, let's say that I have a module foo, and I have a string whose content is "bar". What is the best way to call foo.bar()? I…
ricree
  • 30,749
  • 13
  • 33
  • 27
1451
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
1425
votes
6 answers

Why do Python classes inherit object?

Is there any reason for a class declaration to inherit from object? I just found some code that does this and I can't find a good reason why. class MyClass(object): # class code follows...
tjvr
  • 15,039
  • 6
  • 21
  • 26
1300
votes
27 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
  • 54,865
  • 51
  • 136
  • 180
1245
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
  • 19,078
  • 10
  • 38
  • 69
1058
votes
36 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
  • 10,581
  • 3
  • 13
  • 3
962
votes
28 answers

How can I access and process nested objects, arrays or JSON?

I have a nested data structure containing objects and arrays. How can I extract the information, i.e. access a specific or multiple values (or keys)? For example: var data = { code: 42, items: [{ id: 1, name: 'foo' }, { …
Felix Kling
  • 705,106
  • 160
  • 1,004
  • 1,072
889
votes
22 answers

Why is null an object and what's the difference between null and undefined?

Why is null considered an object in JavaScript? Is checking if ( object == null ) Do something the same as if ( !object ) Do something ? And also: What is the difference between null and undefined?
rahul
  • 174,563
  • 47
  • 223
  • 254
845
votes
23 answers

How do I copy an object in Java?

Consider the code below: DummyBean dum = new DummyBean(); dum.setDummy("foo"); System.out.println(dum.getDummy()); // prints 'foo' DummyBean dumtwo = dum; System.out.println(dumtwo.getDummy()); // prints…
Veera
  • 29,772
  • 35
  • 94
  • 133
834
votes
32 answers

How to pass an object from one activity to another on Android

I am trying to work on sending an object of my customer class from one Activity and display it in another Activity. The code for the customer class: public class Customer { private String firstName, lastName, Address; int Age; public…
kaibuki
  • 16,208
  • 34
  • 75
  • 111
819
votes
15 answers

Dynamically access object property using variable

I'm trying to access a property of an object using a dynamic name. Is this possible? const something = { bar: "Foobar!" }; const foo = 'bar'; something.foo; // The idea is to access something.bar, getting "Foobar!"
RichW
  • 9,214
  • 5
  • 21
  • 31
1
2 3
99 100