Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In JavaScript what does 0 === 4 mean?

Tags:

javascript

I saw the following JavaScript code and it confused me since logically 0 will never be 4 or 5:

 if (0 === 4) { // Safari
      safari.self.addEventListener("message", safariMessageListener, false);
 } else if (0 === 5) { // Opera
      opera.extension.onmessage = operaMessageListener;
 }

I will assume that this code works. So has the programmer managed to redefine 0 in some manner or is 0 somehow not referring to the number 0? Can anyone explain this code and why would anyone code like this? What are the advantages of this technique? (except for confusing people like me!)

For completeness, this snippet came from the YouTube Center Grease Monkey plug-in. The URL of which is:

https://github.com/YePpHa/YouTubeCenter/wiki/Developer-Version

And the full method is:

  function initListeners() {
    if (support.CustomEvent) {
      window.addEventListener("ytc-content-call", eventListener, false);
    } else {
      window.addEventListener("message", messageListener, false);
    }

    window.addEventListener("unload", windowUnload, false);

    if (0 === 4) { // Safari
      safari.self.addEventListener("message", safariMessageListener, false);
    } else if (0 === 5) { // Opera
      opera.extension.onmessage = operaMessageListener;
    }
  }
like image 518
Phil Avatar asked Jan 10 '23 22:01

Phil


1 Answers

The source code looks quite different:

if (@identifier@ === 4) { // Safari
  safari.self.addEventListener("message", safariMessageListener, false);
} else if (@identifier@ === 5) { // Opera
  opera.extension.onmessage = operaMessageListener;
}

There is just an Ant build script which replaces this @identifier@ with ${indentifier.userscript}:

<target name="copy-userscript-meta">
  <copy todir="${buildDir}">
    <fileset dir="${src.meta}/" />
  </copy>
  <antcall target="tokenreplace" />
  <replace dir="${buildDir}" value="${indentifier.userscript}" token="@identifier@" encoding="${encoding}" />
</target>

${indentifier.userscript} is defined in the same file.

like image 101
hsz Avatar answered Jan 19 '23 05:01

hsz