What causes inability for JavaScript to access a property?

My current coding project involves the usage of typescript decorators in the following way:

function logParameter(target: any, key : string, index : number) {
  var metadataKey = `__log_${key}_parameters`;
  console.log(target);
  console.log(metadataKey === "__log_move_parameters");
  console.log(target[metadataKey]); // always undefined
}


class Horse{
    public __log_move_parameters: number[];

    move(@logParameter distanceInMeters = 45) {
        console.log("Galloping...");
    }
}
Horse.prototype.__log_move_parameters = [];
let tom: Horse = new Horse();

tom.move(34);

It's interesting to note that despite having __log_move_parameters in the target, when I check for target[metadataKey], it is always undefined.

To explore further, you can visit the playground

Answer №1

target only receives the property after executing that code. Don't be deceived (like many of us have been, countless times) by the fact that when you print an object to the console, the console has a live connection to the object; when you expand the object in the console, it displays the object's state at the time of expansion, not when it was initially logged.

Here's an updated version with these two lines added:

console.log("target", JSON.stringify(target));
console.log("in?", "__log_move_parameters" in target);

They reveal

target {}
in? false

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Encountered an error in NodeJs with mongoDB: TypeError stating that property 'fullname' is not defined

I am having issues storing user-entered data from an HTML form into MongoDB. The error message I receive is "TypeError: cannot read property 'fullname' of undefined" TypeError: Cannot read property 'fullname' of undefined at C:&bso ...

Ever since I switched to a different monitor, my Javascript has suddenly stopped functioning

I'm encountering an issue where my JS stops working every time I switch displays within a single HTML file. I've attempted to replace the HTML onclick call with a JavaScript function, but the problem persists. Update: Apologies for the unclear e ...

Formik React struggling with error management and handling tasks accurately

I am currently using the Formik template to develop a Login Form. onSubmit={( values, { setSubmitting, setErrors /* setValues and other goodies */ } ) => { props.logMeIn(va ...

Attempting to showcase information on the Angular frontend

When attempting to retrieve the Street name, I am seeing [object Object]. What is the optimal approach for displaying JSON data on the client side? I managed to display a street name but struggled with other components. How can I access the other elements ...

Managing form submissions in React with inputs spread across multiple components

I've been working on a ReactJS project with a form that is divided into multiple components. The main component imports all the child components, each containing input boxes, along with a submit button: My issue lies in trying to get all the componen ...

Comparison of MVC5 and Angular JS: Which is the better framework

For the past year, I've been immersed in ASP.NET MVC and have found joy in building SPA's using tools like: Partial views(via html.action() and renderPartial()) Ajax helpers (Ajax.Actionlink() and Ajax.beginform()) Now, I'm curious ...

Could this be considered a typical trend - opting to return data instead of a promise?

I recently came across a new approach while reading 'Mean Machine'. Typically, I have always been taught to return a promise from a service to the controller and then handle it using .success or .then. In this case, the author is directly retur ...

Chrome fails the karma tests while phantomjs passes them

I've been struggling with this issue for some time now and can't seem to find a solution. I'm working on an Ionic 2 project that utilizes Angular 2's testing environment. When I run 'ng test' using Karma's Chrome launcher ...

Store user input in a paragraph

I want to create a unique program that allows users to input text in a field, and when they click "Start", the text will appear in a paragraph backwards. I plan to use Html, jQuery, and CSS for this project. Can anyone provide guidance on how to achieve th ...

What is the best way to execute a series of asynchronous JavaScript functions one after the other?

As I attempt to call the following functions in succession, their return does not always happen in the expected order. Upon discovering asynchronous functions and the concept of using "callbacks," I realized there might be a solution for executing these f ...

The framework for structuring web applications using Javascript programming

While I have extensive experience in PHP and C# programming, my knowledge of Javascript is limited. When it comes to server side programming, MVC is my preferred method as it allows me to keep my code well-organized. However, when it comes to writing Java ...

Problem encountered while generating a torus shape using webGL

I am currently developing a program that aims to create 3D parametric shapes using webgl. The current code I have works successfully for rendering a sphere, but when I try switching the equations for a torus, only the upper half of the torus is being displ ...

Combining Various Items Retrieved from Fetch Request

I am attempting to merge multiple objects retrieved from an API by grouping them based on their id and values, ensuring that any modifications are only applied to individual objects rather than affecting all of them. Here is my latest approach: const car ...

Setting a JavaScript value for a property in an MVC model

I am currently working on an asp.net mvc application and I am in the process of implementing image uploading functionality. Below is the code for the image upload function: $(document).ready(function () { TableDatatablesEditable.init(); ...

Steps for changing an object into an array

I need help converting an object into an array using pure javascript. Here is the object I want to convert: [{"itemCode":"Mob-mtr"},{"itemCode":"640-chr"}] This is how I want the array to look like: ["Mob-mtr","640-chr","541-mtr"] I've attempted ...

Execute the dynamic key API function

Below is the data object: registration: { step1: { project: '', }, step2: { adres: '', facade: '', floor: '', }, }, An attempt is being ...

Displaying various results using a range slider

I really need some assistance in making this range slider produce multiple outputs. I've attempted a few different options, but unfortunately, I haven't been able to figure it out. My goal is to have the text "590" display as 5.9 times the value ...

Instructions on allowing the user to enter text for searching through an array of objects, and displaying a message if a match is found. Use only pure JavaScript

As a newcomer to JavaScript, I greatly appreciate the support from everyone on this platform. Thank you in advance for your help! I am currently working on building a basic music app as a learning project in JavaScript. The main goal is to understand how J ...

Sending a variable to the resize() function in jQuery

Currently, I am working with two specific divs in my project: "#sidebar-wrapper" and ".j1". The height of ".j1" is dynamic as it changes based on its content. My objective is to set this height value to the "#sidebar-wrapper" element. I have attempted to ...

Visual Studio Code encounters a Node.js error stating "Module not found"

I have been struggling to run my Node.js program in VSCode. Despite trying various solutions found on StackOverflow, none of them seem to be working for me. I even attempted the Json file method, but unfortunately, that didn't work either. internal/mo ...