[javascript] assigning function to variable in PXCE  SOLVED

Forum for the PDF-XChange Editor - Free and Licensed Versions

Moderators: PDF-XChange Support, Daniel - PDF-XChange, Chris - PDF-XChange, Sean - PDF-XChange, Paul - PDF-XChange, Vasyl - PDF-XChange, Ivan - Tracker Software, Stefan - PDF-XChange

Mathew
User
Posts: 726
Joined: Thu Jun 19, 2014 7:30 pm

[javascript] assigning function to variable in PXCE

Post by Mathew »

I'm probably missing something, but if I assign a built-in function to a variable, and try to run that variable, nothing happens and the script is aborted with no error. If I put it in a try catch block, it never gets to the catch, either.

Code: Select all

{
const a = console.println;
a("test");
// script will be aborted above with no output to the console.
}
User avatar
Roman - Tracker Supp
Site Admin
Posts: 337
Joined: Sun Nov 21, 2004 3:19 pm

Re: [javascript] assigning function to variable in PXCE  SOLVED

Post by Roman - Tracker Supp »

Hi Mathew,
When you assign an object member function to a variable, the function loses its binding to that object. println can only work as a member of console object.

Perhaps you want something like this:

Code: Select all

{
const println = console.println.bind(console);
println("test");
}
Mathew
User
Posts: 726
Joined: Thu Jun 19, 2014 7:30 pm

Re: [javascript] assigning function to variable in PXCE

Post by Mathew »

Ahh thank you. Makes total sense. Thanks for taking the time.
- Mathew.
User avatar
Roman - Tracker Supp
Site Admin
Posts: 337
Joined: Sun Nov 21, 2004 3:19 pm

Re: [javascript] assigning function to variable in PXCE

Post by Roman - Tracker Supp »

You are welcome Mathew.