0

I am looking at the source code of the Video Title Adder chrome extension. I am new to javascript and tried to figure out what was the purpose of declaring the following global variable YTTA as an anonymous function:

var YTTA = function () {};

YTTA.URLREGEX = /(?:youtube\.com\/watch\?.*v=|youtu\.be\/|y2u\.be\/)([-_A-Za-z0-9]{11})/i;
YTTA.ATTR_ID = 'data-ytta-id';

[...]

$(document).ready(function () {
chrome.extension.sendMessage({name : 'getOptions'}, function (resp) {
    YTTA.links = resp["textlinks"]*1;
    YTTA.image = resp["imglinks"]*1;

Why not just declare YTTA as YTTA = {};? What is the purpose of the anonymous function in the declaration var YTTA = function () {};.

I tried to google this but could not find a quick answer.

Håkon Hægland
  • 39,012
  • 21
  • 81
  • 174
  • It doesn't ever appear to be called in any way, so yes, I'd agree that it's odd. – deceze Mar 29 '19 at 10:36
  • 2
    Is there any `prototype` stuff in there, maybe later you can do instancing.. `new YTTA()`, – Keith Mar 29 '19 at 10:38
  • this might help you distinguish between the two and their uses: https://stackoverflow.com/questions/4859800/should-i-be-using-object-literals-or-constructor-functions in a general case – Nick Parsons Mar 29 '19 at 10:39
  • Try changing the declaration to `YTTA = {};` Does anything break? I went through the code in the link, and I think that it is a mistake. – Kartik Soneji Mar 29 '19 at 10:48
  • 2
    I got curious, so I looked up the history of that line. It was added in a commit that was a complete rewrite of the script. I couldn't see `YTTA` ever being used as a function - before that commit, it just wasn't there. After the commit, it is used as a simple property holder object. So, I suspect it might be a minor error - perhaps before/during the re-write, the author wanted to have a function for this but then scrapped the idea but forgot to change the code to `{}`. That *should* be the correct value. Not sure if I have the authority to make an answer to this effect. – VLAZ Mar 29 '19 at 13:37

0 Answers0