Closed Bug 488227 Opened 15 years ago Closed 7 years ago

Include variable and object context on property of Function (and global caller) and be able to set

Categories

(Core :: JavaScript Engine, enhancement)

x86
Windows Vista
enhancement
Not set
normal

Tracking

()

RESOLVED INVALID

People

(Reporter: brettz9, Unassigned)

Details

User-Agent:       Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.8) Gecko/2009032609 Firefox/3.0.8
Build Identifier: 

Hello,

In working with http://phpjs.org/ to implement a number of PHP functions in JavaScript, while JavaScript is remarkably flexible and overwhelmingly allows us to implement with full fidelity (to the extent of our humble efforts), I think we've gotten a good idea by now of the features for which we see a recurring need for new functionality in JavaScript in order to be able to implement, and would, I think, make very useful additions to the JavaScript language in general, regardless of the library.

If each Function could contain information on its local variables (or global, if the caller is in the global context), as well as the caller's parent object (at least its 'this' reference) and more info on the calling method, especially if those were writable as well as readable, a great deal of flexibility/functionality could be added.

For example:

function b() {
    arguments.callee.caller.localVars['a'].push('something');
    arguments.callee.caller.inScopeVars['a'].push('another'); // Catch nesting, and global variables as well as local ones, ideally optionally restricting some variables or calls by privacy
    arguments.callee.caller.that.prop = 7; // Ideally 'that' would not need to be defined as 'this' in the calling object, in order to work with the object from the callee here
    // Be able to reflect on names as well 
    alert(arguments.callee.caller.thisObjName); // "myObj"
    alert(arguments.callee.caller.thisObjMethod); // "someMethod" (even though the referenced function itself is anonymous)
}

var myObj = {
    prop : 5,
    someMethod : function () {
        var a = [];
        b();
    }
};
myObj.someMethod();

In our case, such functionality would allow us to implement unset(), extract(), compact(), and other such functions fully, as well as be able to pass objects and arrays fully by reference (e.g., to delete or change the type of the whole passed array/object).

One could then also implement dynamic variables and functions which weren't limited to an already known context such as window (albeit more awkwardly if some other syntax did not work):

    function funcName () {alert("we're in a nested function, called dynamically!");}
    var a = 'funcName';
    arguments.callee.inScopeVars[a]();
    {a}(); // something like this would be even better though

    var a = 'oneVar';
    var b = 'TwoVar';
    var oneVarTwoVar = 5;
    alert(arguments.callee.localVars[a+b]); // 5
    alert({a+b}); // something like this would be better

Greater reflection on the names of the functions/methods and variables (as indicated in the myObj example) would also open up a number of interesting options, such as for documentation and debugging.

Reproducible: Always
Assignee: general → nobody
New features to JavaScript should now be proposed to TC39 [1] before being implemented in SpiderMonkey, therefore closing as Invalid.

[1] https://github.com/tc39/proposals/blob/master/CONTRIBUTING.md
Status: UNCONFIRMED → RESOLVED
Closed: 7 years ago
Resolution: --- → INVALID
You need to log in before you can comment on or make changes to this bug.