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
421
votes
19 answers

How can I create an object and add attributes to it?

I want to create a dynamic object (inside another object) in Python and then add attributes to it. I tried: obj = someobject obj.a = object() setattr(obj.a, 'somefield', 'somevalue') but this didn't work. Any ideas? edit: I am setting the…
John
  • 21,047
  • 43
  • 114
  • 155
417
votes
7 answers

Difference between 'cls' and 'self' in Python classes?

Why is cls sometimes used instead of self as an argument in Python classes? For example: class Person: def __init__(self, firstname, lastname): self.firstname = firstname self.lastname = lastname @classmethod def…
Scaraffe
  • 5,041
  • 5
  • 21
  • 20
417
votes
10 answers

Create an empty object in JavaScript with {} or new Object()?

There are two different ways to create an empty object in JavaScript: var objectA = {} var objectB = new Object() Is there any difference in how the script engine handles them? Is there any reason to use one over the other? Similarly it is also…
Jonas Pegerfalk
  • 9,016
  • 9
  • 29
  • 29
407
votes
14 answers

How do I print my Java object without getting "SomeType@2f92e0f4"?

I have a class defined as follows: public class Person { private String name; // constructor and getter/setter omitted } I tried to print an instance of my class: System.out.println(myPerson); but I got the following output:…
Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
397
votes
6 answers

TypeScript Objects as Dictionary types as in C#

I have some JavaScript code that uses objects as dictionaries; for example a 'person' object will hold a some personal details keyed off the email address. var people = { : <'some personal data'>}; adding > "people[] = ;"…
Robert Taylor
  • 4,225
  • 2
  • 17
  • 9
396
votes
14 answers

Perform .join on value in array of objects

If I have an array of strings, I can use the .join() method to get a single string, with each element separated by commas, like so: ["Joe", "Kevin", "Peter"].join(", ") // => "Joe, Kevin, Peter" I have an array of objects, and I’d like to perform a…
jackweirdy
  • 5,632
  • 4
  • 24
  • 33
394
votes
14 answers

Deleting Objects in JavaScript

I'm a bit confused with JavaScript's delete operator. Take the following piece of code: var obj = { helloText: "Hello World!" }; var foo = obj; delete obj; After this piece of code has been executed, obj is null, but foo still refers to an…
394
votes
15 answers

How to add an object to an array

How can I add an object to an array (in javascript or jquery)? For example, what is the problem with this code? function() { var a = new array(); var b = new object(); a[0] = b; } I would like to use this code to save many objects in the…
naser
  • 4,373
  • 4
  • 18
  • 13
375
votes
19 answers

Sort array of objects by single key with date value

I have an array of objects with several key value pairs, and I need to sort them based on 'updated_at': [ { "updated_at" : "2012-01-01T06:25:24Z", "foo" : "bar" }, { "updated_at" : "2012-01-09T11:25:13Z", …
user1022241
369
votes
12 answers

How to determine whether an object has a given property in JavaScript

How can I determine whether an object x has a defined property y, regardless of the value of x.y? I'm currently using if (typeof(x.y) !== 'undefined') but that seems a bit clunky. Is there a better way?
user213154
355
votes
22 answers

How can I get the index of an object by its property in JavaScript?

For example, I have: var Data = [ { id_list: 1, name: 'Nick', token: '312312' }, { id_list: 2, name: 'John', token: '123123' }, ] Then, I want to sort/reverse this object by name, for example. And then I want to get something like this: var…
rsboarder
  • 4,592
  • 3
  • 20
  • 21
351
votes
9 answers

How to perform .Max() on a property of all objects in a collection and return the object with maximum value

I have a list of objects that have two int properties. The list is the output of another linq query. The object: public class DimensionPair { public int Height { get; set; } public int Width { get; set; } } I want to find and return the…
theringostarrs
  • 11,940
  • 14
  • 50
  • 63
348
votes
19 answers

JavaScript: filter() for Objects

ECMAScript 5 has the filter() prototype for Array types, but not Object types, if I understand correctly. How would I implement a filter() for Objects in JavaScript? Let's say I have this object: var foo = { bar: "Yes" }; And I want to write a…
345
votes
4 answers

How can I create a copy of an object in Python?

I would like to create a copy of an object. I want the new object to possess all properties of the old object (values of the fields). But I want to have independent objects. So, if I change values of the fields of the new object, the old object…
Roman
  • 124,451
  • 167
  • 349
  • 456
345
votes
16 answers

What is the difference between loose coupling and tight coupling in the object oriented paradigm?

Can any one describe the exact difference between loose coupling and tight coupling in Object oriented paradigm?
Jim
  • 4,639
  • 5
  • 27
  • 31