How does the variable propt represent the properties of an object in JavaScript?

How does the variable propt represent the properties of an object in JavaScript? It doesn’t seem to be a built-in method or property. Why does it allow iteration through every property in the object? I’m looking to understand how to javascript loop through object properties.

Using for…in Loop: The for…in loop is a common method to iterate over an object’s properties. In this case, propt can represent each property name in the object.

Here’s an example:

const obj = { name: "Alice", age: 25, city: "Wonderland" };
for (let propt in obj) {
if (obj.hasOwnProperty(propt)) {
console.log(propt + ": " + obj[propt]);
}
}

In this code, propt holds the key of each property in obj, allowing you to access its value with obj[propt].

Just to add up to the above answer :slight_smile:

To ensure the object is of the expected type before casting, you can check if symbolInfo is an instance of MarkerSymbolInfo using instanceof. This prevents any potential runtime errors if the object is not actually of the expected type:

if (symbolInfo instanceof MarkerSymbolInfo) {
    return this.createMarkerStyle(symbolInfo);
} else {
    // Handle the case where symbolInfo is not a MarkerSymbolInfo
}

This approach is a safer way to handle TypeScript casting because it ensures that you’re casting only when the type matches.

Hey

Quite interesting solutions shared by @joe-elmoufak

Here is what worked for me, you can define a custom type guard to check if symbolInfo is a MarkerSymbolInfo. This approach adds clarity and type safety to your code:

function isMarkerSymbolInfo(symbolInfo: SymbolInfo): symbolInfo is MarkerSymbolInfo {
    return symbolInfo.symbolShapeType === "marker";
}

if (isMarkerSymbolInfo(symbolInfo)) {
    return this.createMarkerStyle(symbolInfo);
}

Here, the type guard isMarkerSymbolInfo helps TypeScript narrow down the type of symbolInfo to MarkerSymbolInfo before using it, ensuring safer TypeScript casting.

Hope I was able to help you :slight_smile: