JavaScript Objects and their methods
Objects in JavaScript are non-primitive datatypes ie. they can contain many properties written as name: value pairs. A property's value can be a function, in which case the property is known as a method. Objects are mutable, meaning their contents can be changed, even when they are declared as const
.
Object properties can be accessed in two ways:
objectName.propertyName
or
objectName["propertyName"]
The 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. A non-inherited property can be removed using the delete
operator.
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.
Like arrays, objects also have methods. Methods are actions that can be performed on objects.
'this' keyword:
In JavaScript, this
keyword refers to an object, which object.. depends on how this
is being invoked. Inside an object method, this
refers to the current object, while alone this
refers to the global object. In a function too, this
refers to the global object, but in a function, with strict mode, this
becomes undefined.