473,804 Members | 3,088 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to get the object method name?

It's not difficult getting the function name of the caller but if the
caller is an object's method, how do you get the method name?

<html>

<head>
<script type="text/javascript">
function displayCallStac k(func){

var caller = func.caller;
if(caller == null) return;

console.log(cal ler);
displayCallStac k(caller);
}

function objectA(){
}

objectA.prototy pe.method1 = function(){

test();
}

var obj = new objectA();
function test(){
test1();
}

function test1(){
displayCallStac k(displayCallSt ack);
}
</script>

<body onload="obj.met hod1()">

</body>

</html>

Here is the result:
test1()
test()
function()
onload(event)

Ideally, I like the output to be
test1()
test()
obj.method1()
onload()

Your help is highly appreciated!

Nov 12 '07 #1
7 3872

ja********@gmai l.com wrote:
It's not difficult getting the function name of the caller but if the
caller is an object's method, how do you get the method name?

<html>

<head>
<script type="text/javascript">
function displayCallStac k(func){

var caller = func.caller;
if(caller == null) return;

console.log(cal ler);
displayCallStac k(caller);
}

function objectA(){
}

objectA.prototy pe.method1 = function(){

test();
}

var obj = new objectA();
function test(){
test1();
}

function test1(){
displayCallStac k(displayCallSt ack);
}
</script>

<body onload="obj.met hod1()">

</body>

</html>

Here is the result:
test1()
test()
function()
onload(event)

Ideally, I like the output to be
test1()
test()
obj.method1()
onload()

Your help is highly appreciated!
Well, I'm not sure you can do that. The function you're trying to get
name of, actually hasn't got any
name - it's an anonymous function, a reference to which is stored in a
variable. Using that variable, you call the anonymous function. It is
the same as the following:

var a, b;
a = b = function()
{
// ... now you try to get which variable was used to call the
function
}

a();
b();

I think Javascript hasn't implemented such mechanisms, at least I
don't know of any.

Regards

Nov 12 '07 #2
ja********@gmai l.com wrote:
It's not difficult getting the function name of the caller
Depends. The property you are using is not part of any standard and so
is not supported in all implementations , e.g. not in Opera 9.2.[34].
but if the caller is an object's method, how do you get the method name?
The only possibility that comes to my mind is to iterate through the
enumerable properties of the object and to compare the property access
with the caller reference. The only problem then is to know the object.
[...]
function displayCallStac k(func){

var caller = func.caller;
if(caller == null) return;

console.log(cal ler);
displayCallStac k(caller);
In JavaScript there is the `stack' property for Error objects. You can
throw an exception and then evaluate the property value.

function a()
{
try {
throw new Error();
}
catch (e)
{
window.alert(e. stack);
}
}

function b()
{
a();
}

b();

I don't know about JScript and other implementations , though.

That said, other than for educational purposes, there is no good reason why
one would need to get the stack trace in the script that is debugged. From
the console.log() call I assume you use or at least support Firebug, which
provides the stack trace along with the error message. Just click the "+"
icon next to the error message.
PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
Nov 15 '07 #3
Thomas 'PointedEars' Lahn a écrit :
That said, other than for educational purposes, there is no good reason why
one would need to get the stack trace in the script that is debugged. From
the console.log() call I assume you use or at least support Firebug, which
provides the stack trace along with the error message. Just click the "+"
icon next to the error message.
There is plenty of good reasons to do it. For example, dont confuse your
non technical users with something they can't understand like firebug
and still get realistics error messages with call stack instead of the
usual "It is not working on my computer".

--
laurent
Nov 15 '07 #4
Laurent vilday wrote:
Thomas 'PointedEars' Lahn a écrit :
>That said, other than for educational purposes, there is no good reason why
one would need to get the stack trace in the script that is debugged. From
the console.log() call I assume you use or at least support Firebug, which
provides the stack trace along with the error message. Just click the "+"
icon next to the error message.

There is plenty of good reasons to do it. For example, dont confuse your
non technical users with something they can't understand like firebug
and still get realistics error messages with call stack instead of the
usual "It is not working on my computer".
If users are bothered with code that actually *breaks* at all then it is the
developer's fault for not taking the possibility of failure of a line when
exposed to that execution environment into account. It is irresponsible,
inept and ultimately incompetent behavior to let users be beta-testers,
unless they want to. And in the latter case they will understand how to use
Firebug, or the built-in error console of the HTML user agent, for that matter.
PointedEars
--
"Use any version of Microsoft Frontpage to create your site. (This won't
prevent people from viewing your source, but no one will want to steal it.)"
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>
Nov 15 '07 #5
Thomas 'PointedEars' Lahn a écrit :
Laurent vilday wrote:
>Thomas 'PointedEars' Lahn a écrit :
>>That said, other than for educational purposes, there is no good reason why
one would need to get the stack trace in the script that is debugged.
There is plenty of good reasons to do it. For example, dont confuse your
non technical users with something they can't understand like firebug
and still get realistics error messages with call stack instead of the
usual "It is not working on my computer".

If users are bothered with code that actually *breaks* at all then it is the
developer's fault for not taking the possibility of failure of a line when
exposed to that execution environment into account.
Will you ever open your mind and your eyes to the real world ?

Everything is breaking in one or another way. In real world, clients are
paying for a product (a website, an intranet, anything web related) and
all the time is not used to add new shinny features to the product. Most
of the time is used to work around bugs to finally get a consistent
behavior in the most used browsers. Not talking about accessibility
nightmare.

Well, it's not totally true, few people like me, and probably you I
assume, have plenty of time to learn and play with the languages they
want and create the tools the way they want. But at work, most of my
colleague dont have this luxury. They have dead lines, they have
schedules. They can't afford to test everything in every situations.

So yes, time to time it happens to break. No big deal, the developer
will fix it.
It is irresponsible,
inept and ultimately incompetent behavior to let users be beta-testers,
unless they want to.
So, basically, users with zero technical knowledges can bring nothing to
developers ? Please, open your mind.
And in the latter case they will understand how to use
Firebug, or the built-in error console of the HTML user agent, for that matter.
You don't have much imagination, don't you ?

Perhaps it is someone surveying the work of someone else in a real
situation to, for instance, find a problem. There is plenty of good
reasons, even in debugged scripts. Perhaps not everytime at every moment
and every seconds, but in real world any situation can happen. Just need
an open mind.

I am beginning to wonder, are you just a troll ?

--
laurent
Nov 15 '07 #6
Laurent vilday said the following on 11/14/2007 8:59 PM:
Thomas 'PointedEars' Lahn a écrit :
>Laurent vilday wrote:
>>Thomas 'PointedEars' Lahn a écrit :
That said, other than for educational purposes, there is no good
reason why
one would need to get the stack trace in the script that is debugged.
There is plenty of good reasons to do it. For example, dont confuse
your non technical users with something they can't understand like
firebug and still get realistics error messages with call stack
instead of the usual "It is not working on my computer".

If users are bothered with code that actually *breaks* at all then it
is the
developer's fault for not taking the possibility of failure of a line
when
exposed to that execution environment into account.

Will you ever open your mind and your eyes to the real world ?
If the past 2-3 years are any indication, no.

<snipped explanation of reality>
So yes, time to time it happens to break. No big deal, the developer
will fix it.
Don't try to explain Reality to Thomas, he doesn't comprehend it. It
doesn't fit into his "Perfect Theoretical World".
>It is irresponsible,
inept and ultimately incompetent behavior to let users be beta-testers,
unless they want to.

So, basically, users with zero technical knowledges can bring nothing to
developers ? Please, open your mind.
You sure have lofty goals :)

<snip>
I am beginning to wonder, are you just a troll ?
Laurent, meet Thomas. You have him figured out now :)

--
Randy
Chance Favors The Prepared Mind
comp.lang.javas cript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Nov 15 '07 #7
Laurent vilday wrote:
Thomas 'PointedEars' Lahn a écrit :
>Laurent vilday wrote:
>>Thomas 'PointedEars' Lahn a écrit :
That said, other than for educational purposes, there is no good
reason why one would need to get the stack trace in the script that
is debugged.
There is plenty of good reasons to do it. For example, dont confuse
your non technical users with something they can't understand like
firebug and still get realistics error messages with call stack
instead of the usual "It is not working on my computer".
If users are bothered with code that actually *breaks* at all then it
is the developer's fault for not taking the possibility of failure of a
line when exposed to that execution environment into account.

Will you ever open your mind and your eyes to the real world ?
I live in the real world. I just don't want to live in the kind of "real
world" you want to live in where you don't take responsibility for your actions.
Everything is breaking in one or another way.
Not if properly tested. A feature may not work in all environments but that
is far from *breaking*.
In real world, clients are paying for a product (a website, an intranet,
anything web related) and all the time is not used to add new shinny
features to the product. Most of the time is used to work around bugs to
finally get a consistent behavior in the most used browsers.
Proper testing is what should take place *before* shipping any product.
Not talking about accessibility nightmare.
Red herring. The so-called "accessibil ity nightmare" is not subject to
script errors.

And again, there is no "accessibil ity nightmare" if there are competent
developers. Especially not with ECMAScript implementations in a Web
context where there are plenty *built-in* possibilities for graceful
degradation.
I am beginning to wonder, are you just a troll ?
I am beginning to wonder, are you just a script-kiddie?
Score adjusted

PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f8************ *******@news.de mon.co.uk>
Nov 15 '07 #8

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

Similar topics

6
22540
by: Martin | last post by:
I'd like to be able to get the name of an object instance from within a call to a method of that same object. Is this at all possible? The example below works by passing in the name of the object instance (in this case 'myDog'). Of course it would be better if I could somehow know from within write() that the name of the object instance was 'myDog' without having to pass it as a parameter. //////////////////////////////// function...
2
3161
by: Peter Nofelt | last post by:
Hey all, I am running into an issue. My situation is that I wish to copy the contents of one listbox to an array, sort it by innerText value, and write the sorted options into a new listbox. Currently I am able to do this by assigning the innerText of the old listbox to the innerText of the new listbox. I find this tedious as i have some other attributes that I eventually would need to copy over to the new listbox.
54
4621
by: tshad | last post by:
I have a function: function SalaryDisplay(me) { var salaryMinLabel = document.getElementById("SalaryMin"); salaryMinLabel.value = 200; alert("after setting salaryMinLabel = " + salaryMinLabel.value); } I also have an asp.net object:
3
4237
by: Adam | last post by:
We have a web site that uses .vb for the web pages and .cs for a class module. We are getting the error in .NET 2.0 and VS 2005 beta 2. It does work with .NET 1.1. When trying to access a page that needs the class module I get an error on web site: Object reference not set to an instance of an object Here is where the error is:
1
2147
by: thzero | last post by:
Is there any way to do this without using the ObjectDataSource? (Apologies for the long post) I have a simple data object: public class TestDO { public TestDO() {} public ID { get; set; } public Name { get; set; } public Description { get; set; }
16
2905
by: anonymous.user0 | last post by:
The way I understand it, if I have an object Listener that has registered as a listener for some event Event that's produced by an object Emitter, as long as Emitter is still allocated Listener will stay alive. Is this correct? If this is correct, I've got a problem. Let's say I've got an object Customer that has an PurchaseList (Collection) of Purchase objects. Now, these Purchase objects were pulled from a datasource Datasource. The...
13
2162
by: andrea | last post by:
Sorry for the stupid question, I know, but sometimes is necessary starts from the basic. I don't know how to pass the result of a method generated from a DAL class to a BL class returning the results as it is. I mean, for instance, something like this. namespace DAL {
0
1379
by: Travis Oliphant | last post by:
This post is to gather feedback from the wider community on PEP 357. It is nearing the acceptance stage and has previously been discussed on python-dev. This is a chance for the wider Python community to comment on the proposal. You will find the PEP attached PEP: 357
12
5560
by: Andrew Poulos | last post by:
With the following code I can't understand why this.num keeps incrementing each time I create a new instance of Foo. For each instance I'm expecting this.num to alert as 1 but keeps incrementing. Foo = function(type) { this.num = 0; this.type = type this.trigger(); } Foo.prototype.trigger = function() {
1
7118
by: =?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?= | last post by:
I get the above error in some of the ASP.NET web applications on a server, and I need some help figuring out how to deal with it. This is a rather long post, and I hope I have enough details that someone who bothers to read all of it have some pointers. Note, I have posted the stack trace and the code exhibiting the problem further down so if you want to start by reading that, search for +++ Also note that I am unable to reproduce...
0
10564
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...
1
10308
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
10073
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
9134
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...
0
6846
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
5513
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
5645
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3806
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2981
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.