473,624 Members | 2,252 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Perfect function forwarding

Hello,

Today, I have a function:

function f()
{
}
and am looking for a way of distinguishing, from inside f, whether it
has been called with new, or not.

function f()
{
if( ... )
return new forwardedFuncti on(...);
else
return forwardedFuncti on();
}

Iow, are we [[Call]]ed or [[Construct]]ed. Can't get around it...

Any help appreciated,

Alexis

--
Some domain is free
Dec 27 '05 #1
6 1691
Alexis Nikichine wrote:
--
Some domain is [...]


somedomain.fr is not. Yet.

<URL:http://www.interhack.n et/pubs/munging-harmful/>
PointedEars
Dec 27 '05 #2
Alexis Nikichine wrote in news:43******** *************** @news.free.fr in
comp.lang.javas cript:

[snip]
and am looking for a way of distinguishing, from inside f, whether it
has been called with new, or not.

function f()
{
if( ... )
return new forwardedFuncti on(...);
else
return forwardedFuncti on();
}

Iow, are we [[Call]]ed or [[Construct]]ed. Can't get around it...


var GLOBAL = this;
function f()
{
alert( this === GLOBAL );
}

f();
new f();

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Dec 27 '05 #3
Rob Williscroft wrote:
Alexis Nikichine wrote [...]:
and am looking for a way of distinguishing, from inside f, whether it
has been called with new, or not.

function f()
{
if( ... )
return new forwardedFuncti on(...);
else
return forwardedFuncti on();
}

Iow, are we [[Call]]ed or [[Construct]]ed. Can't get around it...


var GLOBAL = this;
function f()
{
alert( this === GLOBAL );
}

f();
new f();


this == GLOBAL

should suffice. However, either test does not work for functions called
from non-global method context.
PointedEars
Dec 27 '05 #4
Thomas 'PointedEars' Lahn said the following on 12/27/2005 1:27 PM:
Alexis Nikichine wrote:

--
Some domain is [...]

somedomain.fr is not. Yet.

<URL:http://www.interhack.n et/pubs/munging-harmful/>


Are you actually to the point in life where you have nothing better to
do than pedantically whine about a snippet in a signature?

It might not be so bad if you would reference a decent article instead
of an outdate worthless reference.

None of which is relevant or even remotely related to this thread and/or
this Newsgroup.

--
Randy
comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Dec 27 '05 #5
Alexis Nikichine <al************ **@somedomain.f r> writes:
Today, I have a function:

function f()
{
}
and am looking for a way of distinguishing, from inside f, whether it
has been called with new, or not.


Since the only difference visible inside f will be the value of the
"this" operator, this will have to be sufficient. It cannot be
completely safe, but unless someone is deliberatly trying to cheat, it
should be fairly safe. Javascript has no language based protection
against malicious code.

You could try checking whether the value of "this" could be a new
object created with "new f()". E.g., check
this.constructo r == f
or
this instanceof f

I can't see any way of ensuring that f.prototype is the first object
in the prototype chain, so the above can be tricked using, e.g.,

var fake = new f();
// fiddle with fake
f.call(fake); // can't see that fake is not a new object

Indeed, there shouldn't be a way to see it. The "new" operator creates
the object, but the call to "f" to initialize it afterwards is just a
normal function call (calling the [[Call]] method of the function).

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Dec 28 '05 #6
Lasse Reichstein Nielsen wrote:
Since the only difference visible inside f will be the value of the
"this" operator, this will have to be sufficient. It cannot be
completely safe, but unless someone is deliberatly trying to cheat, it
should be fairly safe. Javascript has no language based protection
against malicious code.

You could try checking whether the value of "this" could be a new
object created with "new f()". E.g., check
this.constructo r == f
or
this instanceof f
Oh thanks, I finally settled on:

function f()
{
if( this.constructo r == f )
return new forwardedFuncti on();
else
forwardedFuncti on();
}

The good of it is that no object with f as a constructor will ever
escape that f, and that it breaks none of my code.
I can't see any way of ensuring that f.prototype is the first object
in the prototype chain, so the above can be tricked using, e.g.,

var fake = new f();
// fiddle with fake
f.call(fake); // can't see that fake is not a new object
So, is my f really fooled with this ?

Since fake.constructo r == forwardedFuncti on, which implies
fake.constructo r != f, I guess that forwardedFuncti on will be called,
and not newed. Unless I'm mistaken, of course.

The only "fiddling with fake" that I could find that messed things up
was to hack f construcotr property:

fake.constructo r = f;
f.call(fake); // Ok, gotcha !

And since in my case, f is actually an anonymous inner function, this
fiddling is not possible.

So finally, here is my definite function interceptor, and I claim it is
bullet-proof (but would be interested in being proved wrong):

// richard cornford's "constructWithA rgs":

var constructWithAr gs = (function(){
function Dummy(){ ; }
return (function(fun, args){
Dummy.prototype = fun.prototype;
var tmp = new Dummy;
fun.apply(tmp, args);
return tmp;
})
})();

// precondition: obj[memberFunction] must be a function
function intercept( obj, memberFunction )
{
obj[memberFunction] = function() {
alert( "BEFORE call to " + memberFunction) ;
try
{
if( constructor == arguments.calle e )
return constructWithAr guments( obj[memberFunction], arguments );
else
return obj[memberFunction].apply( this, arguments );
}
finally
{
alert("AFTER call to " + memberFunction) ;
}
}
}

Actually I could make up one bullet:

intercept( this, "Array" );
a = new Array();
alert( a.constructor == Array ); // false !!

but it's not the interceptor that breaks, but the interceptor that
breaks outside code.
Indeed, there shouldn't be a way to see it. The "new" operator creates
the object, but the call to "f" to initialize it afterwards is just a
normal function call (calling the [[Call]] method of the function).


Well, I was imaginating in the wrong direction: I was somehow looking
for a way to determine whether 'this' was a fresh, new and immaculate
object. Dreaming out loud, I suppose.

Cheers,

Alexis
Dec 28 '05 #7

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

Similar topics

0
1772
by: Anu | last post by:
Hi everyone, I have just joined the group. I need your expert comments on my problem urgently. I am trying to establish SSH forwarding for establishing connections from my web server (obsgeosn) to the database server (obsclara) (both are UNIX Sun Solaris boxes). I am able to start a SSH daemon to start listning on one of the ports on webserver. By using following command: %ssh -2 -N -f -L 3307:obsgeosn:3306 obsclara It works and just...
1
425
by: Binod Nair | last post by:
Hi All, I have an ASP.NET appication running on http://xx.xxx.xxxx.xx/aspApplication and I have a domain http://www.mydomain.com registered at godaddy.com for this application.I have setup domain forwarding at goDaddy.com to forward to http://xx.xxx.xxxx.xx/aspApplication. I am using Forms Authentication on the application.Now the problem i am facing is , when i login to the application through http://xx.xxx.xxxx.xx/aspApplication...
1
1625
by: Bob Hairgrove | last post by:
Why can I do this: template<typename A, typename B=A> struct X { /*...*/ }; whereas this gives me an error about an undeclared identifier with MSVC++ 7.1: struct A { A(int arg1, int arg2=arg1); };
1
592
by: Jesse Rosenthal | last post by:
Hello all, I'm writing a script which will backup data from my machine to a server using rsync. It checks to see if I am on the local network. If I am, it runs rsync over ssh to 192.168.2.6 using the pexpect module to log in. That's the easy part. Now, when I'm not on the local network, I first want to open up an ssh connection to do port forwarding, so something like this:
0
1369
by: nicholas | last post by:
I use session variables that stores data for a shopping basket. The site runs at www.mysite.com/france and works fine. But I also have the domainname: www.mysiteforfrance.com So, on a Plesk admin, I set a frame forwarding on www.mysiteforfrance.com to www.mysite.com/france When a visitor goes to www.mysiteforfrance.com he can visit the whole site, but in the adres bar he always sees www.mysiteforfrance.com. (in other words: this is...
0
1111
by: Daniel P. | last post by:
Can anyone advise me for a staring point where I can learn about how to do forwarding? What I need is to learn how to have IIS machine that is exposed to the outside world that does only forwarding. This server will only be a kind of proxy between the outside world (the web services client) and the real web services server.
1
2487
by: thomas | last post by:
Hello all, It seems like subdomain forwarding prevents ASP.Net session state from working correctly. Example: two websites http://www.jgphotographers.com/test and http://picturestore.newpicturestore.com/test - the first one works - the counter using session state increases every time the button is clicked. The second one uses subdomain forwarding with masking. The "picturestore"
1
3411
by: jacob navia | last post by:
Everybody knows that hash tables are fast. What is less known is that perfect hash tables are even faster. A perfect hash table has a hash function and a table layout that avoids collisions completely. You can lookup a key in the table with a single hash function call. What is even less known is that there is a perfect software for generating perfect hash tables called "gperf" offered by GNU INC.
6
3482
by: mcl | last post by:
I have a domain name which is set up for web forwarding with a frame. I have a link on one of the site's pages to an external site. When I select the link the external site is displayed correctly with its own URL in the address bar. When I select the <backbutton in the browser, my domain name appears temporarily in the browser, but it returns to the external site. If I look at back History (In Firefox) there are two entries for my...
1
8341
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8488
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...
0
7170
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6112
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
5570
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4084
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4183
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1793
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1488
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.