Best practice for setting the value of the __proto__ property

After deserializing some objects from JSON, I am looking to set a new prototype for them in order to add getter and setter functions. While the traditional method involves setting __proto__ object properties like function1 and function2, it is worth noting that this approach is considered non-standard and deprecated by MDC as explained here. Is there a more standards-compliant way to achieve the same outcome without creating numerous wrapper objects?

Answer №1

Changing the prototype of an object after its creation is not compliant with standards. However, creating objects with any desired prototype while parsing from JSON is a standards-compliant method.

As stated on http://www.json.org/js.html:

The optional reviver parameter serves as a function that will be invoked for each key and value at every level of the final outcome. Each value will be substituted by the output of the reviver function. This can be utilized to convert generic objects into instances of pseudoclasses, or to convert date strings into Date objects.

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

Capture a snapshot of a webpage that includes an embedded iframe

Currently, we have a nodeJS/angular 4 website that contains an iframe from a third party (powerBI Emebdded). Our goal is to develop a feature that allows the end user to capture a screenshot of the entire page, including the content within the iframe. We ...

Updating the component's state based on the server response

Injecting the props into the initial state of a component is something I'm working on. The goal is to update the state and have the data reflected immediately when a button inside the component is clicked. The eventData object contains two attributes ...

Troubleshooting JSON.Net Deserialization Issue in VB: All Fields Returning Null

I'm struggling to understand why all fields are returning null. I've double-checked my classes and they seem to align perfectly with the JSON structure. I even used the JSON Util tool to convert my JSON result to VB (). Despite attempting the pro ...

The final condition of the ternary operator fails to render if the specified condition is satisfied

Having trouble identifying the issue with this ternary statement. The last element (blurredscreen) is not appearing when authStatus !== "authenticated". return ( <> <div key={"key-" + id}> {isO ...

Preview not showing CSS changes properly

1) I am encountering an issue with displaying CSS in a form preview tab. Despite setting the CSS, it does not reflect on the fields after clicking the preview button. 2) Are there alternative methods to open the tab in a new window rather than opening it ...

What is the most effective way to utilize forms in order to send information from both an input field and a dropdown selection menu with just one button click

Currently utilizing Material UI and React for my project. I am dealing with data from a text field and a drop down select that I want to be 'submitted' when a button is clicked https://i.sstatic.net/fvxo0.png Below is the code with unnecessary ...

An error has been thrown stating that the function startTimer is not defined, despite the fact that it is clearly defined

const startBtn = document.querySelector('.startBtn'); const pauseBtn = document.querySelector('.pauseBtn'); const ResetBtn = document.querySelector('.resetBtn'); const time = document.querySelector('.time'); let sec ...

Triggering a new window on button click with Vue and Nuxt

Having an issue with a button click event in Vue that opens a PDF using window.open(). It's working well on all browsers except for Safari on MAC. Any ideas what could be causing this? Should I pass $event to the method? <div class="button-do ...

Decoding and extracting data from a JSON file

Here's a Python script I'm using to read and parse a JSON file: import json with open('testdata.json', 'r') as raw_data: content = json.load(raw_data) print(content) The JSON data looks like this: {"grp":"1"; "to ...

Issues with navigation drawer not functioning

function adjustMenu() { var navigation = document.getElementById("myTopnav"); if (navigation.className === "topnav") { navigation.className += " responsive"; } else { navigation.className = "topnav"; } } body {margin:0;} ul ...

Encountering issues with styling the search bar using CSS and unable to see any changes reflected

I am currently working on creating a searchBar and customizing its CSS, but unfortunately, most of the styling properties seem to not be taking effect. So far, only changing colors seems to work as expected. .mainBar{ background-color: blue; width: ...

Retrieve all elements from JSON using jQuery

JavaScript: function loadDoc(url) { $.ajax({ url: 'mytestjson', dataType: 'json', cache: false }).success(function (result) { console.log(result); //var txt = result.newBranches[0].newNon ...

Determine if the first statement is executed or the second with a boolean function

When a function returns a boolean value, it triggers 2 conditions and based on the outcome, a specific message is shown For instance: <script> function guessTheNumber(p1) { const test = 5; return p1 > 6 || p1 === test ; } </script> ...

Can JSON data be customized to include a specific decimal precision?

One interesting query involves a web API that returns an amount. Let's consider a simplified example: The API might return values like these: { Amount:40 } { Amount:40.1 } { Amount:40.15 } The question arises: Can we ensure that the A ...

How do you modify the SVG viewport using code?

I am looking to create a feature that allows all images inside an SVG object to be moved. My plan is to use JavaScript, and possibly jQuery, to handle mouse events (down, move, up) in order to change the viewport of the SVG. However, I am currently facing ...

The use of the readonly attribute in an <input> element prevents the widget

Attempting to utilize the https://github.com/Eonasdan/bootstrap-datetimepicker, but facing an issue where making the input field read-only also disables the date picker button, preventing access to the date picker widget. <div class="container"> ...

How to use the transform function in JavaScript

Hey there, I'm looking to create a new function. I am just starting out with javascript. I want to wrap the code below in a function like: ConfirmDelete() { }; This function should perform the same action as this code snippet: $('.btn-danger ...

Visibility of Code in AngularJS

After inspecting the source code of a website built with Angular today, I came across a snippet that made me ponder whether it's advisable to have such elements visible to everyone. ul class="nav-account desktop-only" ng-show="!User.isAuthenticated" ...

What is the best way to extract JSON information from an API request?

This is my current code snippet: <?php $data = file_get_contents("http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=##################################&steamids=76561198040884950"); $json = json_decode($data); ?> <s ...

Breaking Down Oracle Delimited String into Individual Rows

This situation presents a challenge. Currently, we are using Oracle Database 11g, which does not have JSON support. My objective is to divide a JSON string into rows based on a delimiter and then apply filters to each of these rows. Is this achievable? Let ...