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
611
votes
13 answers

What is the difference between `throw new Error` and `throw someObject`?

I want to write a common error handler which will catch custom errors thrown on purpose at any instance of the code. When I did throw new Error('sample') like in the following code try { throw new Error({'hehe':'haha'}); // throw new…
Jayapal Chandran
  • 10,600
  • 14
  • 66
  • 91
543
votes
32 answers

How would one write object-oriented code in C?

What are some ways to write object-oriented code in C? Especially with regard to polymorphism. See also this Stack Overflow question Object-orientation in C.
Dinah
  • 52,922
  • 30
  • 133
  • 149
492
votes
17 answers

How to define an empty object in PHP

with a new array I do this: $aVal = array(); $aVal[key1][var1] = "something"; $aVal[key1][var2] = "something else"; Is there a similar syntax for an object (object)$oVal = ""; $oVal->key1->var1 = "something"; $oVal->key1->var2 = "something…
ed209
  • 11,075
  • 19
  • 66
  • 82
491
votes
20 answers

Python dictionary from an object's fields

Do you know if there is a built-in function to build a dictionary from an arbitrary object? I'd like to do something like this: >>> class Foo: ... bar = 'hello' ... baz = 'world' ... >>> f = Foo() >>> props(f) { 'bar' : 'hello', 'baz' :…
Julio César
  • 12,790
  • 10
  • 38
  • 45
483
votes
35 answers

How to convert an array to object in PHP?

How can I convert an array like this to an object? [128] => Array ( [status] => "Figure A. Facebook's horizontal scrollbars showing up on a 1024x768 screen resolution." ) [129] => Array ( [status] => "The other day at…
streetparade
  • 32,000
  • 37
  • 101
  • 123
480
votes
3 answers

JavaScript object: access variable property by name as string

If I have a javascript object that looks like below var columns = { left: true, center : false, right : false } and I have a function that is passed both the object, and a property name like so //should return false var side =…
Hailwood
  • 89,623
  • 107
  • 270
  • 423
478
votes
36 answers

JavaScript: Object Rename Key

Is there a clever (i.e. optimized) way to rename a key in a javascript object? A non-optimized way would be: o[ new_key ] = o[ old_key ]; delete o[ old_key ];
Jean Vincent
  • 11,995
  • 7
  • 32
  • 24
472
votes
15 answers

Print content of JavaScript object?

Typically if we just use alert(object); it will show as [object Object]. How to print all the content parameters of an object in JavaScript?
cometta
  • 35,071
  • 77
  • 215
  • 324
463
votes
1 answer

How to remove item from a JavaScript object

How can I remove an item from a JavaScript object? Like this: var test = {'red':'#FF0000', 'blue':'#0000FF'}; test.remove('blue');
ezebemmel
  • 4,751
  • 2
  • 16
  • 7
457
votes
8 answers

Reverse of JSON.stringify?

I'm stringyfing an object like {'foo': 'bar'} How can I turn the string back to an object?
thelolcat
  • 10,995
  • 21
  • 60
  • 102
442
votes
2 answers

Creating object with dynamic keys

First off, I'm using Cheerio for some DOM access and parsing with Node.js. Good times. Heres the situation: I have a function that I need to create an object. That object uses variables for both its keys and values, and then return that single…
JDillon522
  • 19,046
  • 15
  • 47
  • 81
441
votes
9 answers

How to create an object property from a variable value in JavaScript?

I want to add a new property to 'myObj', name it 'string1' and give it a value of 'string2', but when I do it it returns 'undefined: var myObj = new Object; var a = 'string1'; var b = 'string2'; myObj.a = b; alert(myObj.string1); //Returns…
ecu
  • 4,433
  • 2
  • 16
  • 4
438
votes
18 answers

Converting a JS object to an array using jQuery

My application creates a JavaScript object, like the following: myObj= {1:[Array-Data], 2:[Array-Data]} But I need this object as an array. array[1]:[Array-Data] array[2]:[Array-Data] So I tried to convert this object to an array by iterating…
The Bndr
  • 13,204
  • 16
  • 68
  • 107
433
votes
22 answers

How to get the size of a JavaScript object?

I want to know the size occupied by a JavaScript object. Take the following function: function Marks(){ this.maxMarks = 100; } function Student(){ this.firstName = "firstName"; this.lastName = "lastName"; this.marks = new Marks(); } Now I…
anonymous
429
votes
12 answers

Remove array element based on object property

I have an array of objects like so: var myArray = [ {field: 'id', operator: 'eq', value: id}, {field: 'cStatus', operator: 'eq', value: cStatus}, {field: 'money', operator: 'eq', value: money} ]; How do I remove a specific one based…
imperium2335
  • 23,402
  • 38
  • 111
  • 190