473,394 Members | 1,843 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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

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
2 1756
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Cylix | last post by:
I just know the form like: myObject.prototype.linkFade = function(link, doShow) { .... .... .... }...
2
by: Jeff | last post by:
Hello, I assigned a new object to a local variable ("req") in a function (see below). The local variable "req" is obviously destroyed when the function exits, but should the object referenced by...
28
by: Larax | last post by:
Best explanation of my question will be an example, look below at this simple function: function SetEventHandler(element) { // some operations on element element.onclick = function(event) {
4
by: alex | last post by:
I am so confused with these three concept,who can explained it?thanks so much? e.g. var f= new Function("x", "y", "return x * y"); function f(x,y){ return x*y } var f=function(x,y){
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.