Javascript Syntax: Objects Flashcards
Restrictions in Naming Properties
JavaScript object key names must adhere to some restrictions to be valid. Key names must either be strings or valid identifier or variable names (i.e. special characters such as - are not allowed in key names that are not strings).
Dot Notation for Accessing Object Properties
Properties of a JavaScript object can be accessed using the dot notation in this manner: object.propertyName. Nested properties of an object can be accessed by chaining key names in the correct order.
Objects
An object is a built-in data type for storing key-value pairs. Data inside objects are unordered, and the values can be of any type.
Accessing non-existent JavaScript properties
When trying to access a JavaScript object property that has not been defined yet, the value of undefined will be returned by default.
JavaScript Objects are Mutable
JavaScript objects are mutable, meaning their contents can be changed, even when they are declared as const. New properties can be added, and existing property values can be changed or deleted.
It is the reference to the object, bound to the variable, that cannot be changed.
JavaScript for…in loop
The JavaScript for…in loop can be used to iterate over the keys of an object. In each iteration, one of the properties from the object is assigned to the variable of that loop.
Properties and values of a JavaScript object
A JavaScript object literal is enclosed with curly braces {}. Values are mapped to keys in the object with a colon (:), and the key-value pairs are separated by commas. All the keys are unique, but values are not.
Key-value pairs of an object are also referred to as properties.
Delete operator
Once an object is created in JavaScript, it is possible to remove properties from the object using the delete operator. The delete keyword deletes both the value of the property and the property itself from the object. The delete operator only works on properties, not on variables or functions.
javascript passing objects as arguments
When JavaScript objects are passed as arguments to functions or methods, they are passed by reference, not by value. This means that the object itself (not a copy) is accessible and mutable (can be changed) inside that function.
JavaScript Object Methods
JavaScript objects may have property values that are functions. These are referred to as object methods.
Methods may be defined using anonymous arrow function expressions, or with shorthand method syntax.
Object methods are invoked with the syntax: objectName.methodName(arguments).
JavaScript destructuring assignment shorthand syntax
The JavaScript destructuring assignment is a shorthand syntax that allows object properties to be extracted into specific variable values.
It uses a pair of curly braces ({}) with property names on the left-hand side of an assignment to extract values from objects. The number of variables can be less than the total properties of an object.
shorthand property name syntax for object creation
The shorthand property name syntax in JavaScript allows creating objects without explicitly specifying the property names (ie. explicitly declaring the value after the key). In this process, an object is created where the property names of that object match variables which already exist in that context. Shorthand property names populate an object with a key matching the identifier and a value matching the identifier’s value.
this Keyword
The reserved keyword this refers to a method’s calling object, and it can be used to access properties belonging to that object.
Here, using the this keyword inside the object function to refer to the cat object and access its name property.
javascript function this
Every JavaScript function or method has a this context. For a function defined inside of an object, this will refer to that object itself. For a function defined outside of an object, this will refer to the global object (window in a browser, global in Node.js).
JavaScript Arrow Function this Scope
JavaScript arrow functions do not have their own this context, but use the this of the surrounding lexical context. Thus, they are generally a poor choice for writing object methods.
Consider the example code:
loggerA is a property that uses arrow notation to define the function. Since data does not exist in the global context, accessing this.data returns undefined.
loggerB uses method syntax. Since this refers to the enclosing object, the value of the data property is accessed as expected, returning “abc”.