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
335
votes
29 answers

Generic deep diff between two objects

I have two objects: oldObj and newObj. The data in oldObj was used to populate a form and newObj is the result of the user changing data in this form and submitting it. Both objects are deep, ie. they have properties that are objects or arrays of…
Martin Jespersen
  • 25,743
  • 8
  • 56
  • 68
333
votes
13 answers

Add a property to a JavaScript object using a variable as the name?

I'm pulling items out of the DOM with jQuery and want to set a property on an object using the id of the DOM element. Example const obj = {} jQuery(itemsFromDom).each(function() { const element = jQuery(this) const name = element.attr('id') …
Todd R
  • 18,236
  • 8
  • 31
  • 39
328
votes
12 answers

Java Serializable Object to Byte Array

Let's say I have a serializable class AppMessage. I would like to transmit it as byte[] over sockets to another machine where it is rebuilt from the bytes received. How could I achieve this?
iTEgg
  • 8,212
  • 20
  • 73
  • 107
324
votes
14 answers

How can I sort arrays and data in PHP?

This question is intended as a reference for questions about sorting arrays in PHP. It is easy to think that your particular case is unique and worthy of a new question, but most are actually minor variations of one of the solutions on this…
deceze
  • 510,633
  • 85
  • 743
  • 889
323
votes
10 answers

How does a ArrayList's contains() method evaluate objects?

Say I create one object and add it to my ArrayList. If I then create another object with exactly the same constructor input, will the contains() method evaluate the two objects to be the same? Assume the constructor doesn't do anything funny with…
Mantas Vidutis
  • 16,376
  • 20
  • 76
  • 92
321
votes
9 answers

What is the JavaScript equivalent of var_dump or print_r in PHP?

I would like to see the structure of object in JavaScript (for debugging). Is there anything similar to var_dump in PHP?
Adriana
  • 8,464
  • 13
  • 36
  • 37
320
votes
37 answers

How do you check if a JavaScript Object is a DOM Object?

I'm trying to get: document.createElement('div') //=> true {tagName: 'foobar something'} //=> false In my own scripts, I used to just use this since I never needed tagName as a property: if (!object.tagName) throw ...; So for the second object,…
Jonathan Lonowski
  • 121,453
  • 34
  • 200
  • 199
319
votes
2 answers

How do I create a dynamic key to be added to a JavaScript object variable

I'm trying something like this, but this example does not work. jsObj = {}; for (var i = 1; i <= 10; i++) { jsObj{'key' + i} = 'example ' + 1; } What can I do to make a dynamic key like this?
Ryan
  • 3,475
  • 2
  • 16
  • 12
318
votes
15 answers

How to get object length

Is there any built-in function that can return the length of an object? For example, I have a = { 'a':1,'b':2,'c':3 } which should return 3. If I use a.length it returns undefined. It could be a simple loop function, but I'd like to know if there's…
Larry Foobar
  • 11,092
  • 15
  • 56
  • 89
316
votes
21 answers

Adding elements to object

I need to populate a json file, now I have something like this: {"element":{"id":10,"quantity":1}} And I need to add another "element". My first step is putting that json in a Object type using cart = JSON.parse, now I need to add the new…
HypeZ
  • 4,017
  • 3
  • 19
  • 34
305
votes
5 answers

Is there a “not in” operator in JavaScript for checking object properties?

Is there any sort of "not in" operator in JavaScript to check if a property does not exist in an object? I couldn’t find anything about this around Google or Stack Overflow. Here’s a small snippet of code I’m working on where I need this kind of…
Aaron
  • 10,386
  • 13
  • 37
  • 53
305
votes
11 answers

How to display all methods of an object?

I want to know how to list all methods available for an object like for example: alert(show_all_methods(Math)); This should print: abs, acos, asin, atan, atan2, ceil, cos, exp, floor, log, max, min, pow, random,round, sin, sqrt, tan, …
GeekTantra
  • 11,580
  • 6
  • 41
  • 55
304
votes
8 answers

How to use "get_or_create()" in Django?

I'm trying to use get_or_create() for some fields in my forms, but I'm getting a 500 error when I try to do so. One of the lines looks like this: customer.source = Source.objects.get_or_create(name="Website") The error I get for the above code…
Stephen
  • 5,959
  • 10
  • 33
  • 43
301
votes
23 answers

How do you load custom UITableViewCells from Xib files?

The question is simple: How do you load custom UITableViewCell from Xib files? Doing so allows you to use Interface Builder to design your cells. The answer apparently is not simple due to memory managment issues. This thread mentions the issue and…
DrGary
  • 3,321
  • 3
  • 20
  • 14
293
votes
9 answers

Fastest way to get the first object from a queryset in django?

Often I find myself wanting to get the first object from a queryset in Django, or return None if there aren't any. There are lots of ways to do this which all work. But I'm wondering which is the most performant. qs = MyModel.objects.filter(blah =…
Leopd
  • 41,333
  • 31
  • 129
  • 167