Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > get object name from within object

Reply
Thread Tools

get object name from within object

 
 
Richard Cornford
Guest
Posts: n/a
 
      07-23-2005
Lasse Reichstein Nielsen wrote:
<snip>
> Still, arguments.callee is the only way to access the
> current function without knowing its name. It's just that
> it only works inside that function, and at that point,
> you should know the name.


Which means that the only circumstances where arguments.callee would be
necessary is when the function is anonymous (the result of an anonymous
function expression or the use of the Function constructor, but only
sometimes), or when the author has (rather stupidly) re-used the
function's name as a formal parameter, local variable or inner function
name and so masked the property on the scope chain that refers to the
function itself.

Richard.


 
Reply With Quote
 
 
 
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      07-23-2005
"Richard Cornford" <(E-Mail Removed)> writes:

> Which means that the only circumstances where arguments.callee would be
> necessary is when the function is anonymous (the result of an anonymous
> function expression or the use of the Function constructor, but only
> sometimes),


.... in which case there is no name to find ...

> or when the author has (rather stupidly)


(probably why I didn't even consider it

> re-used the function's name as a formal parameter, local variable or
> inner function name and so masked the property on the scope chain
> that refers to the function itself.


But still, the original poster's question went to the name of the
function, not the function itself, so going through "callee" is
more work than is normally needed.

I would pass the function name as a parameter to the PHP function that
generates the JS function. It's much safer than trying to deduce it on
the client.

/L
--
Lasse Reichstein Nielsen - http://www.velocityreviews.com/forums/(E-Mail Removed)
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
 
Reply With Quote
 
 
 
 
warteschlange
Guest
Posts: n/a
 
      07-23-2005
Lasse Reichstein Nielsen wrote:
>I would pass the function name as a parameter to the PHP function that
>generates the JS function. It's much safer than trying to deduce it on
>the client.

after thinking over it and a few cup of coffee later , i will keep it
straight and make it from PHP.
thanks to all in the jsOlymp lowering my coffee consumption.
I mean it honestly - js can be hell without help.

 
Reply With Quote
 
Richard Cornford
Guest
Posts: n/a
 
      07-23-2005
warteschlange wrote:
<snip>
> i create dynamic a jsfunction
> [JS]
> function trulyPHPFunc( arguments ){
> return call_HTTP_Request( {func:callee , args:arguments } );
> // where calle is the name
> }
> [/JS]


But when you dynamically create this function you must know its name in
order to output ' function trulyPHPFunc( ... ', so what is stopping you
dynamically inserting the same function name into a string within the
function?

> don't remind me:
> * this works only 'sync' and it can block the browser


It is not 'can block the browser', it is 'will block the browser'.
Blocking the browser is a bad idea that should be avoided. It is
particularly problematic when the network or server experience problems
as it blocks the browser until the request times out.

> * i know AJAX/XML_RPC/SOAP


The statement 'I know AJAX' seems somewhat vague. The 'A' in AJAX stands
for asynchronous so to 'know' AJAX should be to know how to handle
asynchronous background client-server interactions. Granted the term
'AJAX' appears to have been degraded to mean nothing more than using
HTTP request objects, and then quite often synchronously because the
thought and effort involved in coping with asynchronous operations
exceeds the willingness or the knowledge of those interested in applying
a buzzword to their own creations.

Richard.


 
Reply With Quote
 
warteschlange
Guest
Posts: n/a
 
      07-24-2005
Richard Cornford wrote:
>...


what you are saying in other words: keep javascript clean and do all
the crap on the php side.

My aproach was sending a list of functions from php to javascript,
allowed to use. Just a list (getMethods() ) as usual in xml_rpc. This
is an array of strings.
And: if i have a nice working backend, why writing another interface?

So the original question was: how to get a function, with its name as
argument, from a string.
Hey, why not use eval? Answer: I don't like eval at all.
I try to keep it simple and avoid the xml_rpc/soap overhead.

I got all the good answers very quick and came to the conclusion, that
it is easier to solve it in php with a wrapper. But now i'm spending my
time chatting around justifying my aproach.

Just asking a good question and not explaining all the background, does
not automatically mean, that he/she is not aware of the rest. I
thought, pointing out
> don't remind me:
> * this works only 'sync' and it can block the browser
> * i know AJAX/XML_RPC/SOAP

will avoid a discussion about these items.

But i was wrong.

So why not introduce a syntax in the discussons for marking out items
not to be discussed?
example:
my son (9) want to see '''Once upon a time in Mexico'''
question: are there any sequences of violence?
[avoid_topics_in_this_thread]
quality of the movie
why Rodriquez filmed in HDTV
beauty of Hayek, Mendes
[/avoid_topics_in_this_thread]

this will help to focus.

Sync is not a bad idea, it is just bad implemented in the browsers.
(Other languages have a lot better mechanism to fetch a result from a
function without blocking the rest, if needed : vb/c++/j++ ).
[avoid_topics_in_this_thread]
sync/async
[avoid_topics_in_this_thread]


Andres Obrero

 
Reply With Quote
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      07-24-2005
"warteschlange" <(E-Mail Removed)> writes:

> Richard Cornford wrote:
>>...

>
> what you are saying in other words: keep javascript clean and do all
> the crap on the php side.


I concur. You have *full* control of the PHP code and environment,
whereas anything in a web page can be changed by proxies and user
preferences, and will be running in one of a dozen browsers with
slightly-to-greatly different runtime environments.

/L
--
Lasse Reichstein Nielsen - (E-Mail Removed)
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
 
Reply With Quote
 
Richard Cornford
Guest
Posts: n/a
 
      07-24-2005
warteschlange wrote:
> Richard Cornford wrote:

<snip>
> I got all the good answers very quick and came to the
> conclusion, that it is easier to solve it in php with a
> wrapper. But now i'm spending my time chatting around
> justifying my aproach.


Any suggestion of an interest in the names of functions (and
particularly variables that refer to objects and object instances (which
may be functions)) raises the suspicion that the underlying code is
fundamentally flawed in its design. And that a simpler, clearer (often
faster) and more direct alternative could be suggested in exchange for a
sufficient explanation of the problem context.

> Just asking a good question and not explaining all the
> background, does not automatically mean, that he/she is
> not aware of the rest.


Many people assert that they know and understand what they are doing,
while simultaneously making demands/request/assumptions that suggest
that they do not. And when they bother to engage to the point of
allowing others to actually understand what is being done it more often
as not turns out that they were unaware of something significant (and
often advantageous).

> I thought, pointing out
>> don't remind me:
>> * this works only 'sync' and it can block the browser
>> * i know AJAX/XML_RPC/SOAP


You realise that you have attributed that quote to me when you are its
author and not me.

> will avoid a discussion about these items.
>
> But i was wrong.
>
> So why not introduce a syntax in the discussons for
> marking out items not to be discussed?


Because nobody would pay any attention to it at all.

<snip>
> Sync is not a bad idea, it is just bad implemented
> in the browsers.


In an event driven system there is no need to block, so doing so is a
bad idea. But when blocking is known to block _everything_ (even if it
doesn't strictly have to) it should be a more obviously bad idea.

> (Other languages have a lot better mechanism to fetch a
> result from a function without blocking the rest, if
> needed : vb/c++/j++ ).

<snip>

And web browsers have completely viable mechanisms for allowing code to
react to events (including onreadystatechange on XML HTTP Request
objects) and javascript has mechanisms for preserving contexts between
the initiation of requests and the events following the response. So the
extent to which it is necessary to block is extremely questionable.

Richard.


 
Reply With Quote
 
Dr John Stockton
Guest
Posts: n/a
 
      07-25-2005
JRS: In article <dc0omu$4hr$1$(E-Mail Removed)>, dated Sun, 24
Jul 2005 20:03:57, seen in news:comp.lang.javascript, Richard Cornford
<(E-Mail Removed)> posted :

>Any suggestion of an interest in the names of functions (and
>particularly variables that refer to objects and object instances (which
>may be functions)) raises the suspicion that the underlying code is
>fundamentally flawed in its design.


Possibly.

I have a function which, given a variable that addresses a function,
returns the original name of the function. At least, it does for me; I
don't see any reason for it not to work generally. As I use it,
function execution time is not important.

function Primordial() {}
var B = Primordial, C = B, D = FuncName(C)

Variable D is now a String of value " Primordial" (I always either want
the space or don't mind it).

function FuncName(Fn) { return Fn.toString().match(/( \w+)/)[0] }


It's useful in test code; a test routine can be given the function to
test, and use FuncName to discover the its name as a string for display.
Otherwise, one would either pass both the variable and the corresponding
name-string, or pass the name-string and use eval ... .

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
adding a variable name to a hash to name is part of the variable name Bobby Chamness Perl 2 04-22-2007 09:54 PM
print("my name is {name}, and {age}-year old {gender}", name, age, gender); =?iso-8859-1?B?bW9vcJk=?= Java 7 01-02-2006 04:39 PM
Getting name of object instance from within object method Martin Javascript 6 08-20-2004 12:24 PM
name = name.substring(0, name.lastIndexOf('.')); Help please Jack-2 Javascript 3 12-24-2003 04:39 PM
Re: Urgent! how to get object name, method name and attribute name based on the strings? ding feng C++ 2 06-25-2003 01:18 PM



Advertisments