473,698 Members | 2,153 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1770
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.prototy pe = {
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.prototy pe.mul = function(b) {
return this.value * b;
};

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

// user-defined properties of native objects can be deleted again
delete Operand.prototy pe.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...@gm ail.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!
"theoretica lly 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(de mo(2)); // 3

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

window.alert(de mo(2)); // 4
Jun 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
1411
by: Cylix | last post by:
I just know the form like: myObject.prototype.linkFade = function(link, doShow) { .... .... .... } ----------------------------------------------------------------------------------------------------------------------------------- myObject.prototype.linkFade = function(link, doShow) { with (this) { .... ....
2
2217
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 the variable live on? It seems that it does (and I want it to), but is this correct? I thought that local variables (and objects) are destroyed when the function exits? I declared a callback function--function () {
28
4317
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
2126
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
8600
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9155
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9018
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8858
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6517
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4614
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3038
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2322
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1997
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.