Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transform a Javascript Object to Function manually

Tags:

javascript

I am currently learning Javascript. I've read that an object which has an internal member [[Call]] produces function as the result of typeof that object. I wonder whether I can set this internal member in my Javascript code, i.e. is something like this possible? :

function my_foo(){}

var my_obj = {};  // is the ';' an empty statement?

my_obj["[[Call]]"]=my_foo; // in my test, this didn't work

And if it is possible, would this change the result of typeof that object from object to function?

like image 718
A Dude Avatar asked Aug 02 '17 13:08

A Dude


People also ask

How do you turn an object into a string in JavaScript?

Stringify a JavaScript ObjectUse the JavaScript function JSON. stringify() to convert it into a string. const myJSON = JSON. stringify(obj);

Can we pass object to function in JavaScript?

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.

How do you make an object into a string?

We can convert Object to String in java using toString() method of Object class or String. valueOf(object) method. You can convert any object to String in java whether it is user-defined class, StringBuilder, StringBuffer or anything else.

What is f () in JavaScript?

The $F() function returns the value of any field input control, like text boxes or drop-down lists. This is a convenience alias of Form. Element. getValue. The function can take as argument either the element id or the element object itself.


1 Answers

I've read that an object which has an internal member [[Call]] produces function as the result of typeof that object. I wonder whether I can set this internal member in my Javascript code, i.e. is something like this possible?

No. You cannot directly set or modify internal slots of JavaScript objects. That's part of why they're internal slots, not properties. Your code just creates a property called [[Call]], which is unrelated to the internal slot. From the linked spec section:

Internal slots correspond to internal state that is associated with objects and used by various ECMAScript specification algorithms. Internal slots are not object properties and they are not inherited. Depending upon the specific internal slot specification, such state may consist of values of any ECMAScript language type or of specific ECMAScript specification type values. Unless explicitly specified otherwise, internal slots are allocated as part of the process of creating an object and may not be dynamically added to an object. Unless specified otherwise, the initial value of an internal slot is the value undefined. Various algorithms within this specification create objects that have internal slots. However, the ECMAScript language provides no direct way to associate internal slots with an object.

There's no mechanism defined to let you set [[Call]]. (This isn't universally true of all internal slots; for instance, there's a mechanism that lets you set the [[Prototype]] internal slot of an object: Object.setPrototypeOf. But there isn't one for [[Call]].)

like image 75
T.J. Crowder Avatar answered Sep 19 '22 18:09

T.J. Crowder