By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,717 Members | 1,233 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,717 IT Pros & Developers. It's quick & easy.

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

P: n/a
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!
Jun 27 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
DanYan wrote:
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
Jun 27 '08 #2

P: n/a
VK
On Jun 5, 3:43 am, DanYan <bytecraf...@gmail.comwrote:
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
Jun 27 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.