> For the complete documentation index, see [llms.txt](https://openai.gitbook.io/code-cheatsheets/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://openai.gitbook.io/code-cheatsheets/js/collections/objects.md).

# Objects

Dicts are objects, access members with \['name'] or .name

```javascript
var person = {}; //{} 
var person1 = new Object();
```

## Iterate Over

```javascript
Object.keys(p).map(k => )
Object.entries(p).map(([k, v]) => )
Object.values(p).map(v => )

for (let key of Object.keys(players)) {
    //.......
}
```

## General

### Check in

```javascript
'x' in {'x': 1} //true
```

### Combine Objects

Objects/keys on the right override those to the left

```javascript
{...x, ...y, ...z, a: 1}
Object.assign(x, y, z) //
```

### Copy

```javascript
{...x}
Object.assign({}, x)
```

### Delete

```javascript
delete myObject.prop;
```

### Equality

\=== will do ptr equality

Use lodash .isEqual for deep equality

## Old Classes

```javascript
function Person(name) {
  this.name = name;
  this.greeting = function() {
    alert('Hi! I\'m ' + this.name + '.');
  };
}

var person1 = new Person('Bob'); //new returns obj
var person2 = new Person('John'); //contains a new definition of gretting
```

To define functions out of scope,

```javascript
function Test(a, b, c, d) {
  // property definitions
}

// First method definition
Test.prototype.x = function() { ... };

// Second method definition
Test.prototype.y = function() { ... };
```

Can always add new values

```javascript
person1.newv = "secretsss";
```

## Prototypes?

objects can have a **prototype object** that all instances inherit from, everyone inherits from object prototype

```javascript
Person.prototype
Object.prototype
```

Changes to prototype affect all people

```javascript
Person.prototype.farewell = function() {
  alert(this.name.first + ' has left. Bye for now!');
};
person1.farewell();
```

## Create

`Object.create` makes a new object with the object given as its prototype, basically defining a subclass

```javascript
var person3 = Object.create(person1);//copy constructor, (inherit from person1)
```

## Inheritance

Its a mess, stupid javascript didn't want to do this

```javascript
function Teacher(first, last, age, gender, interests, subject) {
    Teacher.prototype = Object.create(Person.prototype); //inherit from Person.prototype
    Teacher.prototype.constructor = Teacher; //you just replaced the constructor, add it back
    Person.call(this, first, last, age, gender, interests); //.call allows you to pass this setting it

  this.subject = subject;
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://openai.gitbook.io/code-cheatsheets/js/collections/objects.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
