# Objects in js ## Define an object `var objectname = new Object()` or `var objectname = { }` In [[JavaScript]], objects do not need to be explicitly defined as objects, as in the first snippet. The enclosing `{}` around properties makes a variable an object. ## Defining properties A property can be defined within an object using the `:` operator. ```js var Creature = { Origin: function () { RandomOrigin.Get(); }, Race: "half-elf" } ``` The code above defines an object, `Creature`, that has two methods `Origin` and `Race`. The `Origin` method calls a function, `RandomOrigin.Get()`, to return a random value for the origin. Methods can also be defined using `.`: ```js var o = { r: 'some value', t: 'some other value' }; is functionally equivalent to var o = new Object(); o.r = 'some value'; o.t = 'some other value'; ``` _Source: [StackOverflow](https://stackoverflow.com/questions/418799/what-does-colon-do-in-javascript)_ ## `this` keyword `this` is a keyword that refers to different things depending on where it's used, but it generally refers to the parent object. > It has different values depending on where it is used: > In a method, this refers to the owner object. Alone, this refers to the global object. In a function, this refers to the global object. In a function, in strict mode, this is undefined. In an event, this refers to the element that received the event. Methods like call(), and apply() can refer this to any object. > Source: [W3Schools](https://www.w3schools.com/js/js_this.asp) ## References