Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9074563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T18:39:58+00:00 2026-06-16T18:39:58+00:00

Looking at the doT.js source: https://raw.github.com/olado/doT/master/doT.js What does this do? (function(){ return this ||

  • 0

Looking at the doT.js source:

https://raw.github.com/olado/doT/master/doT.js

What does this do?

(function(){ return this || (0,eval)('this'); }()).doT = doT;

To me it looks like it’s creating a global var, window.doT. If that’s all it’s doing, then why not:

window.doT = doT;

?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T18:39:59+00:00Added an answer on June 16, 2026 at 6:39 pm

    It’s getting a reference to the global object, in order to assign doT to it. This is generally done because with a JavaScript library/framework/etc, its one global identifier needs to be exposed to the outside world.

    As for why it’s not simply window.doT = doT;, it’s because the global object isn’t always window, for example, in a non-browser environment. It’s also possible to have window assigned to somewhere else at the point this code is executed.

    How it works

    If this is already truthy, for example, an object such as window, it will return that. It’s likely it will be window (at least in the browser), as a plain function call should have its ThisBinding set to the global object. Otherwise, it will execute eval() in the global scope because an indirect call to eval() will set its scope to global, as opposed to the calling environment’s scope.

    To achieve an indirect call, you have to invoke eval() indirectly, i.e. you can’t just call it with eval(). You can use (0, eval) to invoke it. This relies on the comma operator returning the last evaluated expression, in this case eval. It doesn’t matter what the preceding operands are. Similarly, (0||eval)() would work.

    As for why the body is this, that is the argument to eval(), that is the code to be executed as a string. It will return the this in the global scope, which is always the global object.

    It’s not really relevant nowadays, but in older IEs, you’d need to use execScript() to execute code in the global scope. I can’t remember exactly what versions of IE this was necessary for.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for something similar to Javascript's arguments array: function parent(){ child.apply(this.arguments); } I'm
Looking into Kohana documentation, i found this really usefull function that they use to
I was looking for a JavaScript template engine, and chose DoT.js (mostly because it's
Looking to do a bit of refactoring... Using NHibernate I have this query currently
Looking for best advice on how to do this: I have an insert like
I'm looking for a way to make eclipse shows a dot-line (or any kind
i'm looking for a way to trigger SuperTab when writing a . (dot) in
I was looking at a piece of code today and notice that this particular
Can anybody figure out how to get the .avi URL of a veehd[dot]com video,
I was reading the answer to this question: Haskell: difference between . (dot) and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.