Objects Everything you need to know about the JavaScript Object ๐ Functions The Console ๐ The JavaScript object is a collection of key-value pairs, similar to a map, dictionary, or hash-table in other programming languages. Anything that is not a JS primitive is an Object. An Object is a collection of properties. A Property is a key-value pair that contains a name and a value. A Property Name is a unique value that can be coerced to a string that points to a value. A Property Value can be any value, including other objects or functions, that associated with the name/key. Object Basics Creation Create an an empty object. You have several options. file_type_js_official objects.js // literal const dog = { } // constructor const cat = new Object(); // static method const horse = Object.create({ }) Get and Set Properties Now that we have an empty object, we need to add properties to it using accessors. Valid property names include anything that can be coerced to a string, but must not used reserved words like function, var, return and so on. get = object.property; object.property = set; A potential gotcha with dot notation is that you can only access names that follow variable name identifier conventions, i.e without spaces or that do not start with a digit. obj['hi mom'] = 1; obj[23] = 1; // syntax errors obj.hi mom obj.23 Since ES6, we have a convenient shorthand for setting properties: let hello; let world; // Old way ๐ฉ const obj = { hello: hello, world: world } // Modern way ๐ const obj = { hello, world, } Use a variable or expression as a property name by wrapping it in brackets - this is called a computed property. const x = 'howdy'; const obj = { [x]: 23 } obj.howdy // 23 Object properties can be removed with the delete keyword. delete obj.hello; delete obj.world; References An object is stored in the heap memory, which means variables maintain a reference to it, as opposed to a full copy of it. When checking for object equality, it checks the reference - not the actual value of properties. const original = { } const x = original; const y = original; x === y; // true x === {}; // false Any variable that points to that reference can set its properties and they will be shared between all variables. x.hello = 'world'; original.hello; // world y.hello; // world Combine Objects But what if we want to clone an object to create a separate reference? Object.assign allows us to copy an object’s properties and create a new reference. Its properties will be copied to the new object, thus changes to the original object will not affect the clone. const original = { hello: 'world' } const clone = Object.assign({ }, original); clone === original; // false original.hello = 'changed!'; clone.hello; // world (did not change) Spread Syntax A more concise alternative to Object.assign is the spread syntax. const clone = Object.assign({ }, original); const sugar = { ...original }; const sugar = { ...original, hola: 'mundo' }; Object Methods When a function is assigned to an object, it is called a method. Shorthand const obj = { hello() { console.log('yo') } } obj.hello(); This In a normal method, this refers to the object on which it is defined. const obj = { username: 'Jeff', hello() { console.log(`My name is ${this.username}`) } } obj.hello(); // My name is Jeff Arrow Functions using the arrow syntax are not bound to this, so it refers to the outer or global this context. const obj = { username: 'Jeff', hello: () => console.log(this.username) } obj.hello(); // My name is undefined Chaining In certain JS libraries you will see method chaining with obj.doThis().toThat(), which is made possible by simply returning the value of this from each method. file_type_js_official jquery.js const game = { hitpoints: 100, log() { console.log(`๐พ ${this.hitpoints}`); }, takeDamage() { this.hitpoints -= 10; this.log(); return this; // Required for chaining }, heal() { this.hitpoints += 10; this.log(); return this; // Required for chaining }, } game.takeDamage().takeDamage().takeDamage().heal(); ๐พ 90 ๐พ 80 ๐พ 70 ๐พ 80 Constructors Constructors are just functions that describe how to create an Object. function Boat(name) { this.name = name; this.created = Date.now() this.horn = function () { console.log(this.name) } } The object is then instantiated with the new keyword. const sally = new Boat('Sally'); const molly = new Boat('Molly'); sally.horn() // Sally Chapters What is JavaScript, really? ๐ 1 The History of JavaScript ECMAScript's evolution over the last 25 years free โ๏ธ 2 How JavaScript Works Key terms and concepts related to the inner-workings of JS free Becoming a JS Developer ๐ 3 How to Run JavaScript Code How and where to run JavaScript code free ๐ง 4 The JavaScript Survival Guide A quick primer for advanced JavaScript concepts like primitives, hoisting, closures, and this binding. free ๐คน 5 Functions Key concepts and practical examples of JavaScript functions free ๐งฑ 6 Objects Everything you need to know about the JavaScript Object free Useful JS Concepts to Know ๐ง 7 The Console Go beyond console.log() like and debug like a pro free ๐ฆ 8 Modules Learn how to import and export JS code as a module or package. free JS Interview Prep ๐ณ 9 Graph Traversal Graph Traversal with breadth-first (BFS) and depth-first Search (DFS) free Backend 10 Node.js Quickstart Learn the basics of Node.js in 7 easy steps free ๐ฆ 11 Deno Quickstart A new TypeScript-first JavaScript runtime free