Enhance numbers with properties in ES2015 strict mode

Is there a way to achieve the following scenario?

console.log(a, typeof a); // prints "3 'number'"
console.log(typeof a.mymethod()); // prints  'foobar'

In non-strict mode this can be done (at least in Node), but strict mode in ECMAScript 2015 prohibits setting properties on primitive values (source).

Can I simulate this behavior using Proxy, property descriptors, or any other technique?

Answer №1

This code snippet demonstrates the usage of ES2015 / strict mode and seems to work:

'use strict';

var a = 3;
Number.prototype.mymethod = function() { alert("foobar") };

a.mymethod();

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

Utilizing Spiderable within Meteor results in the replication of head content before it is presented in the body tags

Having trouble with my meteor site, thought it was Google indexing, now suspecting an issue with the Spiderable package. Meteor version 1.1.0.3 is in use, along with spiderable package and gadicohen:phantomjs as suggested by meteorpedia. The current issu ...

discovering the nearest preceding sibling that includes the class .myClass

I have multiple <tr> elements, some of which contain a <td> with the class class="myClass" and some do not. Here is an example of how it may look: <tr> <td class="myClass"></td> <td></td> </tr> <tr> & ...

The JSONP script encountered an error due to an unexpected colon

I am looking to fetch some game information from Steam using an API, however when I try to use it, I encounter the following error: No 'Access-Control-Allow-Origin' header is present on the requested resource. Additionally, when trying to use J ...

The tooltip in Amcharts malfunctions when incorporating the DurationAxis

I've been grappling with getting tooltips to work in amCharts4 when using a DurationAxis(). It seems like there might be a bug, as the tooltips sometimes get stuck in the top left corner. Here's an example: https://jsfiddle.net/jamiegau/fpye3bkv ...

What significance does it hold when an unhandled rejection event lacks a reason field?

Our app tracks client-side errors using Rollbar, but we keep encountering a not very helpful error message from Safari and Chrome: [unhandledrejection] error getting `reason` from event Upon investigation, I found that this message is generated by Rollbar ...

Combining two JSON objects with sailsjs-nodejs to create a single merged object

Hello everyone, I am a beginner with Sailsjs-Nodejs. Currently, I have two JSON Objects in my controller that I need to merge/join in order to create a third object to send as a response. The output when using res.send(obj1) is: [ { total_fare: "37 ...

Angular: check all boxes

My challenge lies in adding a 'select all' checkbox to the header of a table within an existing Angular application. Despite my efforts, I have not been able to achieve the desired functionality. I've managed to get close to success with thi ...

Implementing HTML5 form validation without a submit button and using a personalized error message

I am facing an issue with displaying the HTML5 inline validation bubble without the use of a submit button. The catch is, I am not allowed to use jQuery, only vanilla JavaScript. Here is the HTML I am working with: <a id="send" href="#">Send</a& ...

Using npm as a build tool to merge files together

Upon discovering the flexibility of using npm as a task runner instead of gulp or grunt, I have successfully implemented various tasks such as linting, stylus processing, jade compilation, uglifying, and even watching files. However, I am facing difficulti ...

Ancient queries carry the weight of time

Extremely ancient, very old, incredibly aged beyond belief ...

utilize regular JavaScript to manage Vue.js events beyond the component's scope

There is a VueJs component embedded in a regular web page. Whenever the component triggers an event, I need the regular web page to react accordingly. Is this achievable? The Sites.vue component is a single file element placed within the content of the re ...

What is causing the while loop in Mongodb to repetitively insert the same document rather than cycling through the documents?

Currently, I am in the process of redesigning a MongoDB database structure where the reviews for a specific product will be integrated into the product document itself, rather than maintaining separate collections for products and reviews. I have created a ...

What is the best way to retrieve localstorage information within the getStaticProps function in next.js?

Having trouble retrieving local storage data with the following code. localData = { id: localStorage.getItem("id"), token: localStorage.getItem("token"), }; This snippet is housed within a function called getStaticProps ...

What is the best way to retrieve the elements stored within the 'this' object I am currently manipulating?

How can I access the elements nested within the 'this' that I am currently operating on? Below is the HTML code that I am currently working with: <div class="expander" id="edu">educational qualifications <ul class="list"&g ...

Tips for retrieving the data attribute from a clicked a href tag using jQuery

I am facing an issue with passing the ID to the API URL in AJAX. When I click on the a href tag, the success function does not receive any response. First, I need to fetch all the albums, and that part has already been successfully completed. Here is the ...

Parcel is not compatible with basic HTML and TypeScript files

Just recently, I successfully installed parcel by executing the command: npm install -g parcel-bundler Everything went smoothly. Following that, I created a basic HTML file: <html> <body> <script src="./src/index.ts">< ...

Why isn't jQuery working properly for showing/hiding elements?

I am attempting to create a functionality where the string remove field can be toggled to show or hide using a single button, depending on its current state. Initially, it should be hidden (using the hidden HTML attribute), and upon clicking the button, it ...

Live AJAX inquiries in progress

Is there a way to track the number of active AJAX requests in jQuery, Mootools, or another library similar to Prototype's Ajax.activeRequestCount? I am looking for a method that can be used across different libraries or directly through XMLHttpRequest ...

What is the best way to incorporate keyboard shortcuts into carousel operations using jQuery?

The example can be found here. I want to be able to navigate through the images using the left and right arrows on my keyboard. How can I achieve this using jQuery or CSS? This is the structure of the HTML: <div id="slider-code"> <a cla ...

Using jQuery, generate a dynamic form to create a multidimensional array

I've set up a form where additional dropdowns can be dynamically added if the user clicks on a specific link. Here's an example of how it looks: <div class="dynamic-sale"> <select name="sizes[]" id="sizes" class="entry-dropdown"&g ...