Is there a Javascript syntax similar to a "method group"?

One interesting syntactical feature of C# is the ability to pass in a "method group" to a function expecting a delegate type, like this:

"string".Count (Char.IsWhiteSpace);

This is much cleaner compared to using an anonymous function:

"string".Count (c => Char.IsWhiteSpace (c));

In Javascript, the syntax for anonymous functions can be quite noisy. I would like to achieve a similar simplicity for anonymous functions in Javascript:

var name = "foobar".replace (/^\w/, function (c) { return c.toUpperCase (); });

I have experimented with different approaches using call and apply on the functional form of String.prototype.replace, but it seems that the string argument passed in (c in the example) is not correctly used as this within toUpperCase.

Although I can wrap the function as I did before, I am curious if there is a way to achieve method group application or properly invoke the function on matched characters without encountering errors.

Answer №1

If you want to reference the function that is part of the String object, you can use this method:

"foobar".replace (/^\w/, String.toUpperCase );

Visit this link for more information.

Note: This only works in Firefox at the moment.

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

Is the jqm flipswitch with the label on the left and the switch on the right?

My goal is to display multiple flipswitches on a mobile app page. This is the code I am using: <div class="ui-content"> <form> <fieldset> <div data-role="fieldcontain"> <label for="checkbox-based-flipswitch" ...

Rendering on the server using react-router v4 and express.js

I've been working on implementing server-side rendering with the latest version of react-router v.4. I found a tutorial that I followed at this link: . However, when I refresh the browser, I encounter the following error: Invariant Violation: React.C ...

Having issues with ajax posting functionality

Is the following form correct? <form> <input type="submit" value="X" id="deleteButton" onclick="ConfirmChoice(<?php echo $id; ?>)" /> </form> Here is the function associated with the form: <script type='text/javasc ...

Deactivate all mouse notifications on the given control

My graph control visually represents data points, with each point being plotted as 1 pixel. However, when the number of data points exceeds a certain threshold or the window size is increased, the performance of the plotting decreases significantly when mo ...

How to pass information to a component when it is clicked in React.js

We currently have 2 distinct components: Test1 and Test2. I'm interested in finding out how we can transfer data from the Test1 component to the Test2 component when a click event occurs. One approach could involve using a function similar to this: ...

Display a CSS box continuously

I have a code for a dropdown function that is working smoothly. Now I would like the comment box to show permanently when clicked and close when clicked outside of it. Currently, when I click on the comment, the box shows up but if I try to input a commen ...

Is it possible to target only those divs within a class that wrap to a new line?

I currently have a series of 7 to 12 divs with the same styling, all floated to the left. Is there a CSS selector that can target the ones flowing onto a second row? I doubt this is achievable with standard CSS, so I'm curious if anyone has any jQuery ...

Possible rephrased version: "Encountering a Jquery clash

It appears that the issue causing my problem may be a Jquery conflict. Please correct me if I am wrong after reviewing the information below. I am new to Jquery and attempting to add a dropdown plugin to a website. The attempt is successful, but an existi ...

What could be causing the 405 error in my post request?

While attempting to send a post request to Firebase through my Vue app, I keep encountering an error illustrated in this image. I have a webpack server running and the website is on localhost:8080. However, I also have a live version hosted on hostinger a ...

Unraveling Loops in Data Structures through Two-Step Deserialization

I am currently working with a Serializer/Deserializer that utilizes the PreserveReferencesHandling = PreserveReferencesHandling.All property. The challenge I am facing revolves around circular references within my code. Allow me to provide a simple examp ...

Having trouble retrieving POST data in NodeJS from Angular application

I'm struggling to retrieve the parameters from a POST method implemented in an Angular application. Despite searching for solutions and even attempting examples found online, I have not been successful. Below is the Node.js code snippet: var express ...

The accumulation of keydown events in VueJs

Currently, I am developing a feature where a <textarea> field starts to fade out as soon as the user begins typing using v-on:keydown. However, if the user continues typing, the fading effect resets to keep the opacity: 1. Unexpectedly, the behavior ...

Having trouble with catching errors in try/catch using Async/Await in JavaScript?

I've encountered an issue with the JavaScript code snippet below while using async/await in our ES6 project. I've observed that a 404 response code is not triggering the catch block as expected. Moreover, the .json() method is causing a console e ...

unable to load the ajax file

While setting up Ajax, I encountered an error message in IE that reads: Description: An issue occurred during the processing of a configuration file needed to handle this request. Please check the specific error details below and adjust your configuration ...

Step-by-step guide on how to prioritize rendering the login page before the ngView in AngularJS in order to

As I begin my journey with Angular JS, I am eager to implement security measures in my application. My intention is to set up a log-in page as the default landing page for my single page application. Can anyone provide guidance or recommendations on how ...

The ultimate guide to personalizing group titles in Angular UI-Select

Is there a way in Angular ui-select to customize the group label? I want to make it larger than the selection items as shown in the image below. https://i.stack.imgur.com/ofcak.png The list is currently grouped by country, but how can I adjust the size o ...

How to run 'npm' scripts externally in package.json on Windows?

In the world of development, running arbitrary commands using npm run is a common practice. This involves adding a scripts hash to your package.json: "scripts": { "build-js": "browserify browser/main.js | uglifyjs -mc > static/bundle.js" } To exec ...

Unfortunately, I am unable to utilize historical redirection in React

When an axios request is successfully completed, I want to redirect. However, I am encountering an error that looks like this: https://i.sstatic.net/irTju.png Below is the code snippet: import React, { useState, Fragment } from "react"; import S ...

What is the best way to trigger the second function on a click event?

Is there a way to trigger my 20 second timer with the click of a button, followed by an alert after the timer ends? HTML <aside class="start-box"> <button type="submit" class="btn btn-primary btn-lg" id="toggleBtn" onclick="startCloc ...

By default, Nuxt 2.15.7 is automatically installed when you create a new Nuxt app

I am looking to develop a Nuxt app (version 3) using Vue. However, when I run the command npm create nuxt-app@version mousa, it automatically installs Nuxt2. How can I install Nuxt3 instead with this command? ...