Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert an "object" into a function in JavaScript?

JavaScript allows functions to be treated as objects--if you first define a variable as a function, you can subsequently add properties to that function. How do you do the reverse, and add a function to an "object"?

This works:

var foo = function() { return 1; }; foo.baz = "qqqq"; 

At this point, foo() calls the function, and foo.baz has the value "qqqq".

However, if you do the property assignment part first, how do you subsequently assign a function to the variable?

var bar = { baz: "qqqq" }; 

What can I do now to arrange for bar.baz to have the value "qqqq" and bar() to call the function?

like image 435
mjs Avatar asked Sep 23 '08 22:09

mjs


People also ask

Can you pass an object to a function in JavaScript?

To pass an object to a JavaScript function, we can add a parameter that accepts an object. const someFunc = (arg) => { alert(arg. foo); alert(arg. bar); }; someFunc({ foo: "This", bar: "works!" });

How do you convert an object in JavaScript?

Use the JavaScript function JSON. parse() to convert text into a JavaScript object: const obj = JSON. parse('{"name":"John", "age":30, "city":"New York"}');

How do you create a function for an object?

Function objects can also be created as part of an object literal. Below we create an object named circle with a property named area which is a function object. Next, let's look at an example where a function object is passed around like a regular object. The negate function takes a function as its argument.

Can a JavaScript object have a function as a property?

Methods—setting functions as properties of objects. In JavaScript, you can use functions as values, just like numbers, strings, and objects. That means you can pass them as arguments, return them from other functions, and set them as properties of objects.


1 Answers

It's easy to be confused here, but you can't (easily or clearly or as far as I know) do what you want. Hopefully this will help clear things up.

First, every object in Javascript inherits from the Object object.

//these do the same thing var foo = new Object(); var bar = {}; 

Second, functions ARE objects in Javascript. Specifically, they're a Function object. The Function object inherits from the Object object. Checkout the Function constructor

var foo = new Function(); var bar = function(){}; function baz(){}; 

Once you declare a variable to be an "Object" you can't (easily or clearly or as far as I know) convert it to a Function object. You'd need to declare a new Object of type Function (with the function constructor, assigning a variable an anonymous function etc.), and copy over any properties of methods from your old object.

Finally, anticipating a possible question, even once something is declared as a function, you can't (as far as I know) change the functionBody/source.

like image 179
Alan Storm Avatar answered Sep 30 '22 17:09

Alan Storm