The essence of the argument becomes muddled when implementing bind apply with arrays as arguments

I referenced this particular solution to create a new instance of a class by passing array arguments using the code snippet below:

new ( Cls.bind.apply( Cls, arguments ) )();

However, I encountered an issue where one of my arguments is an array and the values are not preserved during construction.

You can see an example illustrating this problem in this CodePen demo

In the demo, I pass an array named properties as the third argument:

var properties = [
    { name: "first", value: "1" },
    { name: "second", value: "2" },
    { name: "third", value: "3" }
];

Yet, in the output, the properties are shown as undefined.

It seems like there is an error occurring somewhere in the process, but what exactly is going wrong and why?

Answer №1

Your code is on the right track, but make sure to include an extra argument in your factory() function call:

factory(undefined, name, description, properties)

This tip can be found in the response you provided from Stack Overflow:

The 'anything' parameter may not seem necessary, as the new keyword resets f's context. However, it must be included for syntax purposes. In regards to the bind call: It's important to pass a variable number of arguments, which is achieved with this line: var f = Cls.bind.apply(Cls, [anything, arg1, arg2, ...]); result = new f();

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

Determine the SVG arc connecting two given points

My situation involves having coordinates for points X and A, A',... as follows: X [x,y] (starting point) A [a,b], A'[a',b'], etc. (ending points) I also have information about angles XCA, XCA', etc. However, I am struggling t ...

Guide on incorporating a URL link into an <a class=> tag

Working on a Wordpress website, I encountered an issue in the widget section where I can't get any URL code to work! Here is the current code snippet; [one_third] <a class="home-buttons" href="http://example.com/mortgage-calculator"><i clas ...

Using JavaScript, is there a way to modify a JSON parameter within an API?

My API provides JSON data structured like this: [{"NAME":"john","SURNAME":"johny","ADULT":"3","CHILD":"3","BABY":"0",}] In my JavaScript function, I need to send a request to a web service that will update the value of "BABY" from "0" to "1". Can this be ...

What is the proper way to retrieve an array in the render method using React?

As someone who is still fairly new to React, I encountered a coding issue and stumbled upon this particular code snippet from a previous question (I attempted to reach out to those who had previously answered, but unfortunately, received no response as the ...

Solving synchronization issues when updating and selecting MySql data in a Node.js environment with concurrent multiple requests

Currently, I'm using expressJS with an endpoint that connects to a MYSQL database. This endpoint executes a query to retrieve data and assigns the first result to a user by updating a specific field with the user ID. The rule is that each row can onl ...

I seem to be having trouble locating the correct file location

Apologies for the simplicity of my question. I've been struggling to include the find.js file in my articles.js file. Despite trying multiple variations, I can't seem to get the pathname right and haven't found a solution online. Can someon ...

JavaScript post method is failing to work, while it successfully runs in POSTMAN

Attempting to send a message to Android devices through a POST request to Firebase Cloud Messaging. When using POSTMAN, the server responds with a success response 200. However, when attempting the same operation with JavaScript using an AJAX request, I r ...

What methods can be used to transmit binary information from a Node.js socket.io server to a web browser?

After extensively searching the Socket.IO documentation, I am still unable to locate a straightforward example of how to send binary data between a server and client. Despite their assurances that it is included, I have yet to find a minimal demonstration. ...

At times, the Angular Modal Dropdown may unexpectedly open in an upward direction

Dealing with an AngularJS modal that contains a dropdown menu. The menu list is quite extensive. Most of the time, around 70%, the menu drops down in the lower direction which is fine. However, approximately 30% of the time, the dropdown menu appears in ...

<dt> and <dd> have not been added after the previous element

I can't seem to figure out why the dt and dd elements are not being added to the dl I created. Here's my initial attempt: // Dictionary list var words = [ { term: "Procrastination", definition: "Pathological tendency to s ...

Is there a way to display the output of jQuery in an input box rather than a span?

I need to display the result of this function in an input box instead of a span tag because I plan to utilize the result in the following form. function checkChange(){ $.ajax({ url: "ordercalculate.php", data: $('form').seria ...

function called with an undefined response from ajax request in React

Hello, why am I getting the response "callback is not defined"? Component 1: console.log(getUserGps()); Component 2: import $ from 'jquery' export const getUserGps = () => { $.ajax({ url: "https://geolocation-db.com/jsonp", ...

The email sending feature of the ajax jquery form has unexpectedly ceased functioning

I'm encountering an issue with a form that was previously able to send emails from the server, but suddenly stopped working. I'm having trouble identifying the cause of the problem. Interestingly, when I use the form without ajax, the email funct ...

Exploring mongoDB with the "find" query

I am encountering an issue with my MongoDB database. Here is the structure of the data: { "_id" : ObjectId("585fe33d3c63b4a81e00002b"), "class" : [ { "name" : "class 1", "people" : [ { "id" : "58596", ...

Using jQuery each with an asynchronous ajax call may lead to the code executing before the ajax call is completed

In my jQuery script, I utilize an each loop to iterate through values entered in a repeated form field on a Symfony 3 CRM platform. The script includes a $.post function that sends the inputted value to a function responsible for checking database duplicat ...

The PHP function http_build_query() might produce an inaccurate URL when used with an array as a

While working on a dynamic search engine, I noticed an unexpected behavior with the PHP function http_build_query(). In my project, I have a search form with array parameters that are being used. When I tried to retrieve the query URL using http_build_que ...

Vertical stability bar

I need help creating a vertically fixed navigation bar for my website. Currently, I am using a method that has been discussed in various posts: HTML: <html> <head> src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">< ...

Finding the total value of elements within a vector by utilizing intervals

Imagine I have a vector called vect = [1 2 3 4 5 6 7 8 9] and another vector named intervals = [1 3 6 9]. Can I create a new vector consisting of the partial sums of elements in vect, using intervals? I am looking to achieve something like this: Partial s ...

Error encountered: Unspecified "from" address in the provided or default options

Seeking guidance on a project related to Ethereum and Solidity, part of Udemy's course titled "Ethereum and Solidity: The Complete Developers Guide." I am currently working on building the front-end for a Kickstarter alternative. I am facing an issue ...

Steer clear from using the implicit 'any' type while utilizing Object.keys in Typescript

I have a unique situation where I need to loop over an Object while maintaining their type without encountering the error "Element implicitly has an 'any' type because 'ContactList' has no index signature". Despite extensive discussion ...