Is it possible to get a function's scope chain?

D

DanYan

So I was doing some stuff in Javascript, and I want to get access to a
function's scope chain. As a simplified example of what I actually am
trying to do, suppose I have this:


function add(b) {
return function(a) {
return a + b;
};
}

var add3 = add(3);
assert(5 == add3(2));


I want to be able to define a function mul3() that executes in the
same context as bar. I want to do something like this:


function mul3() {
return a * b; // I realize that no b is in static scope here -
see below
}
mul3.scope = add3.scope;

assert(6 == mul3(2));


It doesn't appear that functions have a scope property. Is there any
way at all to do this? Is there any theoretically standards-compliant
way to do it? Thanks!
 
T

Thomas 'PointedEars' Lahn

DanYan said:
So I was doing some stuff in Javascript, and I want to get access to a
function's scope chain.

You are trying to recreate the execution context in which a Function
object was created instead.
As a simplified example of what I actually am trying to do, suppose I have this:

function add(b) {
return function(a) {
return a + b;
};

}

var add3 = add(3);
assert(5 == add3(2));

I want to be able to define a function mul3() that executes in the
same context as bar.

Since the execution context in which the anonymous Function object was
created does not exist anymore, I doubt that is possible.
I want to do something like this:

function mul3() {
return a * b; // I realize that no b is in static scope here -
see below}

mul3.scope = add3.scope;

assert(6 == mul3(2));

It doesn't appear that functions have a scope property.
Correct.

Is there any way at all to do this? Is there any theoretically
standards-compliant way to do it?

There is a practically standards-compliant way to do it: native
objects.

function Operand(a)
{
this.value = a;
}

Operand.prototype = {
add: function(b) {
return this.value + b;
}
};

var three = new Operand(3);

assert(5 === three.add(2));

// new properties can be added to native objects on the fly
Operand.prototype.mul = function(b) {
return this.value * b;
};

assert(6 === three.mul(2));

// user-defined properties of native objects can be deleted again
delete Operand.prototype.mul;

// TypeError: three.mul is not a function
assert(6 === three.mul(2));


HTH

PointedEars
 
V

VK

So I was doing some stuff in Javascript, and I want to get access to a
function's scope chain. As a simplified example of what I actually am
trying to do, suppose I have this:

function add(b) {
return function(a) {
return a + b;
};

}

var add3 = add(3);
assert(5 == add3(2));

I want to be able to define a function mul3() that executes in the
same context as bar. I want to do something like this:

function mul3() {
return a * b; // I realize that no b is in static scope here -
see below}

mul3.scope = add3.scope;

assert(6 == mul3(2));

It doesn't appear that functions have a scope property. Is there any
way at all to do this? Is there any theoretically standards-compliant
way to do it? Thanks!

"theoretically standards-compliant" would be not using pseudo-
inheritance over closures. I consider it absolutely awful and ugly way
to do things from any OOP point of view, either class-based or
prototype-based. Yet mutable closure-constructors are still in some
fashion - though thanks Gog lesser and lesser - so you may and
probably will disregard this advise.

Fot your question itself you may use call() and apply() methods to get
back into the MCC:

function outer(arg_outer) {
return function inner(arg_inner, overload) {
if (!!overload) {
arg_outer = overload;
}
return arg_outer + arg_inner;
};
}

var demo = outer(1);
window.alert(demo(2)); // 3

window.alert(demo.call(outer,2,2)); // 4

window.alert(demo(2)); // 4
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top