- Published on
Objects - The Basics
6 mins
- Authors
- Name
- Juleshwar Babu
Table Of Contents
- Garbage Collection
- Object methods, “this”
- Constructor, operator "new"
- Symbol type
- Object to Primitive conversion
- Content Updates
Source link: https://javascript.info/object-basics
Garbage Collection
JavaScript engine’s garbage collection algorithm is called “mark-and-sweep”. It marks the top level objects referred by global as roots and recursively visits objects referred by the roots. Basically it forms a reference tree. It then sweeps any objects not present in this tree. Browsers apply more optimization over this algo like incremental collection, idle-time collection and generational collection to make it practically viable.
Object methods, “this”
The value of
this
is evaluated during the run-time, depending on the context. The value of this is the “object before the dot” of the property being referencedlet user1 = { name: "Daniel" } let user2 = { name: "Hikaru" } let sayName = function() { window.alert("Hi, my name is ", this.name} } user1.introduceYourself = sayName user2.introduceYourself = sayName user1.introduceYourself() // Hi, my name is Daniel user2.introduceYourself() // Hi, my name is Hikaru
this
for a global object results inundefined
when in strict mode and the global object when not in strict mode 😲function sayHi() { alert(this); } sayHi(); // undefined in strict mode sayHi(); // global object in non-strict mode
Constructor, operator "new"
The main purpose of constructors is to implement reusable object creation code.
function User(name) { // this = {}; (implicitly) // add properties to this this.name = name; this.person = true; // return this; (implicitly) } new User("Hikaru") new User("Arjun")
Constructors do not have a return statement. But if there is a return statement, then the rule is simple:
- If
return
is called with an object, then the object is returned instead ofthis
. - If return is called with a primitive, it’s ignored.
- If
#til
let user = new User; // <-- no parentheses // same as let user = new User();
Symbol type
By specification, only two primitive types may serve as object property keys:
- string type, or
- symbol type.
Otherwise, if one uses another type, such as number, it’s autoconverted to string. So that
obj[1]
is the same asobj["1"]
, andobj[true]
is the same asobj["true"]
.Symbols are “primitive unique values”
let id1 = Symbol("id"); // "id" is just a description for the Symbol let id2 = Symbol("id"); alert(id1 === id2); // false
for..in
loops orObject.keys()
skips over Symbol keys in objectsTechnically, symbols are not 100% hidden.
- There is a built-in method Object.getOwnPropertySymbols(obj) that allows us to get all symbols.
- Also there is a method named Reflect.ownKeys(obj) that returns all keys of an object including symbolic ones.
Object to Primitive conversion
JavaScript uses something called hints to decide what to convert an object to
let user = { name: "John", money: 1000, [Symbol.toPrimitive](hint) { alert(`hint: ${hint}`); return hint == "string" ? `{name: "${this.name}"}` : this.money; } }; // conversions demo: alert(user); // hint: string -> {name: "John"} alert(+user); // hint: number -> 1000 alert(user + 500); // hint: default -> 1500
Content Updates
- 2023-06-08 - init