1

Good I have this function that I need to access the contents of the variable called permalink using console.log() but I can not get it I jump that the variable permalink is not defined. But when I take away the! of! function If I can access permalink, I'm new to this, thank you in advance for your help and comments that may be useful to me. This I need to do without editing the function since it is from an external source This is my code.

!function(PriTwo, document) {
  var DocumentProtocol =
    document.location.protocol != 'https:' &&
    document.location.protocol != 'http:'
      ? 'https:'
      : document.location.protocol;
  var permalink = DocumentProtocol + '//google.com';
  var permalink_two = DocumentProtocol + '//facebook.com';
};

console.log(permalink);

I need that console.log is out of the function, it can not be inside. Any ideas?

  • You can't access outside function unless you return it from function or define in global scope. return value from function and than access – Code Maniac Mar 10 '19 at 05:16
  • Could you please teach me how to do it, please – Benito Herrera Mar 10 '19 at 05:17
  • Read this [Return](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return) – Code Maniac Mar 10 '19 at 05:19
  • I can not edit the function since it is an external code, What do I do then – Benito Herrera Mar 10 '19 at 05:22
  • 1
    If this is the complete external code, then I don't see what it's doing. Just creating some variables. Post enough relevant code so that we can help you. Also, it's not clear how you're calling this function as well. – maazadeeb Mar 10 '19 at 05:44

1 Answers1

3

you have created Immediately-Invoked Function Expression (IIFE) but still, you are not adding parenthesis in the end so function not called yet. for more information about IIFE read this.

also, you can not access a local variable declared inside a function you either need to specify permalink as a global variable. here is a link for a scope in js

For more about Self-executing anonymous function click here. Try this.

    var permalink='';
    var permalink_two='';
    !(function() {
    console.log('dd');
    var DocumentProtocol =
        document.location.protocol != 'https:' &&
        document.location.protocol != 'http:'
          ? 'https:'
          : document.location.protocol;
          console.log('a');
      permalink = DocumentProtocol + '//google.com';
      permalink_two = DocumentProtocol + '//facebook.com';
     
    })();

    console.log(permalink);
    console.log(permalink_two);

or you can return the array from inside the simple function, and access array using the key.

function PriTwo() {
            var DocumentProtocol = (document.location.protocol != "https:" && document.location.protocol != "http:") ? "https:" : document.location.protocol;
            var permalink=[];
            permalink.push(DocumentProtocol + '//google.com');
            permalink.push(DocumentProtocol + '//facebook.com');
             return permalink;
   }
  var link = PriTwo();
  console.log(link[0]);
  console.log(link[1]);
Jigar
  • 3,055
  • 1
  • 32
  • 51
  • I can not edit the function since it is an external code – Benito Herrera Mar 10 '19 at 05:21
  • better add some point what the op missing.What you have done?Its gain your reputation and more help full to understand op and future visitor – prasanth Mar 10 '19 at 05:23
  • I already said that I am something new in javascript I just want to get the value of the permalink variable using console.log and that's what I've done and I came here for some ideas – Benito Herrera Mar 10 '19 at 05:26
  • @BenitoHerrera let me know if you have confusion. – Jigar Mar 10 '19 at 05:39
  • No friend, no! I can not edit it works, and your solution is editing it and so I can not do it, try to remove the permalink but from the function that I have published, not the one you modified – Benito Herrera Mar 10 '19 at 05:58
  • @BenitoHerrera updated answer, actually you are using IIFE function please review updated answer – Jigar Mar 10 '19 at 06:22
  • and there is no need to pass argument but still you can pass if you want to – Jigar Mar 10 '19 at 06:23