How can I assign a property value for a class in one cell from another cell within the same notebook using Observablehq?

I'm brand new to .js classes and my goal is to set a property value for a class that I define in one cell, from within another cell in the same notebook. The cells only contain the code shown here.

Below is the test class in cell 1:

class hist_class {
  constructor() {
    this.test_value=null;
  }
  
  set test_value(newvalue) {
    this.test_value=newvalue;
  }

}

Here is my attempt to "set" in another cell in the same notebook:

test1 = {

const svg = d3.create("svg")
    .attr("width", 100)
    .attr("height", 50);
  
const hist1 = new hist_class();
hist1.test_value = "test value";;

svg.append("text")
    .text(hist1.test_value)
    .attr("x", 10)
    .attr("y", 20);

return svg.node();
  
}

The error message I encounter is "test1 = InternalError: too much recursion." Since my code closely resembles standard examples (e.g. mozilla, w3schools), I believe the issue lies in how observable processes my code rather than being an actual .js error.

Any advice would be greatly appreciated!

Reason behind my interest:

I am striving to replicate the elegant histogram showcased at this link. However, the chart in this example notebook utilizes items defined in cells outside of the main chart section within the same notebook. My aim is to generate multiple instances of the chart without repeating all separate cells. For this reason, I decided to consolidate all supporting cells into a single class to simplify the external code to just one line. Then within the chart's code, I could create new instances of the class and assign values to its properties as needed.

Ultimately, I prioritize bundling and managing the code effectively. If there is a better .js/observable approach to achieve this besides creating classes, I am open to suggestions.

Thank you once again!

Answer №1

The issue with the recursive error occurring in this scenario is due to the utilization of a setter, leading to a loop that keeps calling itself. This problem can be easily replicated using regular JavaScript in various platforms like a browser, node, deno, or any other similar environment. The root cause lies in the naming similarity between the setter and the property, triggering an endless recursion loop. To resolve this issue, simply eliminate the unnecessary setter:

class sample_class {
    constructor() {
        this.demo_value = null;
    }
}

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

Here are steps to prevent swiping fancybox slides using mouse movement

Currently utilizing fancybox 3, I am looking to disable the swipe function for fancybox slides triggered by mouse movement. My preference is to only have the control buttons (next/prev) available. Is there a way to achieve this? Thank you. ...

Utilizing Vue.js to dynamically populate one multiple select based on another multiple select's

Can I create two multiple select fields where the options in the second select menu are based on what is chosen in the first select? Clarification: Each category has various associated descriptions. The initial select menu, named "categories", contains ...

Switch the glyphicon based on the open/closed state of the collapsible element - Bootstrap

Is there a way to update the glyphicon based on whether or not a collapsible element is open? This is the code I currently have: <div class="jumbotron ref" data-toggle="collapse" data-target="#collapse"> <div class="row"> <div class=" ...

Exploring the intricacies of Knockout JS mapping nested models using fromJS function

I am struggling with understanding how to effectively utilize the Knockout JS Mapping Plugin. My scenario involves nested models, and currently I am only using the ko.mapping.fromJS() in the parent model. However, I have noticed that the computed values ar ...

When an array is pushed into another array in JavaScript, it reverts back to its original values

My implementation of recursion involves trying different pieces in a matrix: function solve(matrix, pieces) { // Get next -1 let nextEmpty = getNextEmtpy(matrix); if (!nextEmpty) { console.log(matrix) solutions.push(matrix); ...

Adding up table rows created using AngularJS with the help of jQuery

It might seem a bit strange, but I have a scenario where I need to use jQuery to sum all rows in a table. In this particular table, I'm utilizing angularjs to display a list of users, but I want to calculate the total sum of all rows using jQuery. Th ...

Importing the .css file within a React component for dynamic styling

In my component, I am trying to dynamically load a .css file based on the result of an AJAX call in componentDidMount(). I have attempted adding a <link> element in the render return (which did not work), and also tried injecting the tag directly int ...

The function you are attempting to call, ReactHtmlParser, is not recognized as

I'm currently using react-html--parser in combination with Next.js to convert an HTML string into actual HTML code. Here's my code snippet: import dynamic from "next/dynamic"; const ReactHtmlParser = dynamic( () => { retu ...

Issue with accessing this.props when using withStyles and React Router in React Material UI

When it comes to accessing { classes }, the behavior differs between the Parent Component and the Child Component. In the Parent Component, this.props is defined and can locate { classes } without any issues. However, in the Child Component, this.props app ...

In JavaScript, utilize an object that is defined outside of a function

Here is a JavaScript function I have: function status(){ this.functionA = function(){} //Other functions and fields go here } Now, there is another function as well: function create(root){ var server = library(function (port) { //Additional functi ...

Video tag with centered image

For a current project, I am in need of rendering a centered image (a play button) at runtime on top of a video based on the UserAgent. If the userAgent is not Firefox, I want to display the image as Firefox has its own playEvent and button on top of the vi ...

Checking the API every 20 seconds and halting upon receiving a certain response

In my project, I need to continuously call an API every 20 seconds starting from when the component is rendered. The API calls should only stop when a specific response is received. Here's a little more detail: The API is a GET method that returns a ...

ajax memory leakage

Encountering a gradual memory leak issue in Internet Explorer and Firefox while utilizing a mix of ASP.NET AJAX and jQuery. The situation mirrors the one portrayed on this post: Preventing AJAX memory leaks, but with jQuery and ASP.NET AJAX instead of prot ...

Create bouncing objects with Three.js when clicking the mouse

Hello everyone, I recently started diving into Web Gl and I'm using Three js. One of my first projects involved creating a simple rotating cube in space. Now, I want to take it a step further and add some animation to the cube. For example, I'd ...

Troubleshooting an issue with importing a Component in ReactJS using material-ui

Using the material-ui library, I attempted to create a Table following the code provided in the Custom Table Pagination Action example. However, I encountered the following error: Error Encountered: Warning: React.createElement: type is invalid -- expect ...

Having trouble with the dropdown multiselect feature in AngularJS?

I'm striving to develop a straightforward multi-select dropdown utilizing angular JS, bootstrap, and JS. This dropdown should display days (mon, tue...sun), with options for select all and unselect all. My goal is to create a controller that will de- ...

Can you provide an update on the progress of the JavaScript target development in antlr3?

What is the current status of Antlr3's Javascript target? I attempted to generate a parser for a simple grammar, but encountered numerous compiler errors in the generated code. After investigating the website, I downloaded the latest antlr3.5-snapshot ...

I am unable to access a certain piece of data directly, as I must use an intermediary variable in the process

As someone who is just starting out in programming, I may not have the best questions. Please bear with me if I am taking up your time unnecessarily. I have a query regarding the usage of labels indirectly in DatatoBarChartJS (using a "label") that confuse ...

Passing properties from the parent component to the child component in Vue3JS using TypeScript

Today marks my inaugural experience with VueJS, as we delve into a class project utilizing TypeScript. The task at hand is to transfer the attributes of the tabsData variable from the parent component (the view) to the child (the view component). Allow me ...

Add items to a fresh record using Mongoose and Express

In my model, I have an array of objects that I want to populate with new items when creating a NEW document. While I have found information on how to achieve this using findAndUpdate, I am struggling to figure out how to do it with the save() method. This ...