Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>I have a method, that perfectly works in Firefox, with which I can determine the name of an instance of a particular javascript object</p> </blockquote> <p>I don't think you do, because that's not possible in JavaScript. JS is a call-by-value language; when you write:</p> <pre><code>var temp= 'hello'; getVarName(temp); </code></pre> <p>that's exactly the same as saying:</p> <pre><code>getVarName('hello'); </code></pre> <p>At which point the reference to ‘temp’ as a variable is lost. What I'm guessing your getVarName function does is basically this:</p> <pre><code>function getVarName(value) { for (var name in window) { if (window[name]===value) return name; } } </code></pre> <p>This will work on IE and other browsers without Object.hasOwnProperty(); it will simply return the name of any global variable that matches the argument. The hasOwnProperty() call can be added to this function to refine it a little by only allowing direct properties of window (which act as global variables, including the ones you set explicitly), and not of any of its prototypes. I'm guessing that's what your version of the function is doing, but in practice it has very little effect since almost nothing inherits into ‘window’ by prototype.</p> <p>You're confusing things a little bit by boxing your 'hello' in an explicit String object (which is very unusual and rarely a good idea), which makes it possible to have two different 'hello' objects that are disinguishable using the === identity comparator, so this will work:</p> <pre><code>var a= new String('hello!'); var b= new String('hello!'); getVarName(a); // 'a' getVarName(b); // 'b' - distinguishable object from a </code></pre> <p>But that still doesn't stop you from doing:</p> <pre><code>var a= new String('hello!'); var b= a; getVarName(a); // 'a' or 'b', depending on implementation getVarName(b); // the same 'a' or 'b' as in the previous call </code></pre> <p>So, whilst you can fairly harmlessly lose the hasOwnProperty() call as above, what you're doing can't really work properly and you should probably look at a better way of achieving whatever it is you're up to.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload