Monday, August 5, 2019

Understanding closure in Javascript



I'm trying to wrap my head around closures in Javascript.



Here is an example from a tutorial:



function greeter(name, age) {
var message = name + ", who is " + age + " years old, says hi!";


return function greet() {
console.log(message);
};
}

// Generate the closure
var bobGreeter = greeter("Bob", 47);

// Use the closure
bobGreeter();



The author said that this is an effective way of using closure to make private variables, but I don't get the point.



Could someone enlighten the benefits of coding like this?


Answer



A closure is a pair of a function and the environment in which it was defined (assuming lexical scoping, which JavaScript uses). Thus, a closure's function can access variables in its environment; if no other function has access to that environment, then all of the variables in it are effectively private and only accessible through the closure's function.



The example you provided demonstrates this reasonably well. I've added inline comments to explain the environments.




// Outside, we begin in the global environment.
function greeter(name, age) {
// When greeter is *invoked* and we're running the code here, a new
// environment is created. Within this environment, the function's arguments
// are bound to the variables `name' and `age'.

// Within this environment, another new variable called `message' is created.
var message = name + ", who is " + age + " years old, says hi!";

// Within the same environment (the one we're currently executing in), a

// function is defined, which creates a new closure that references this
// environment. Thus, this function can access the variables `message', `name',
// and `age' within this environment, as well as all variables within any
// parent environments (which is just the global environment in this example).
return function greet() { console.log(message); };
}


When var bobGreeter = greeter("Bob", 47); is run, a new closure is created; that is, you've now got a new function instance along with the environment in which it was created. Therefore, your new function has a reference to the `message' variable within said environment, although no one else does.




Extra reading: SICP Ch 3.2. Although it focuses on Scheme, the ideas are the same. If you understand this chapter well, you'll have a good foundation of how environments and lexical scoping work.



Mozilla also has a page dedicated to explaining closures.


c# - How to get the list of properties of a class?

How do I get a list of all the properties of a class?

Batch Script using Powershell to download from the web

Being that most of my work is done remotely, I have a batch script which downloads all my tools from my FTP server to the client's PC. I just copy and paste the text into notepad on the client's PC and save it as a batch and run it.



This works great, but the problem is, the programs I use get updated every so often - so I would be downloading old versions.
By doing a little research, I found that PowerShell can download files directly from the web.



Download a file via HTTP from a script in Windows



How to execute powershell commands from a batch file?




It seems that all the pieces are there, but I don't know anything about PowerShell at all and I'm a total amateur with batch. Further, i guess you cant just call up PowerShell in the batch script and start writing in PowerShell.



So here is the end goal;
To write a batch script which uses powershell to download files from different websites (i would provide URLs). Instead of downloading the files from my FTP server, the script would download the programs right from the source.



Thank you so much in advance.

What does PHP keyword 'var' do?



This is probably a very trivial question, but I haven't been able to find the answer neither through web search engines, nor on php.net. Please just direct me to where I can read about this, if you haven't got time to explain.





  1. What does the 'var' keyword mean in PHP?

  2. Are there any differences between PHP4 and PHP5?


Answer



It's for declaring class member variables in PHP4, and is no longer needed. It will work in PHP5, but will raise an E_STRICT warning in PHP from version 5.0.0 up to version 5.1.2, as of when it was deprecated. Since PHP 5.3, var has been un-deprecated and is a synonym for 'public'.



Example usage:



class foo {

var $x = 'y'; // or you can use public like...
public $x = 'y'; //this is also a class member variables.
function bar() {
}
}

What are the differences between a pointer variable and a reference variable in C++?

I know references are syntactic sugar, so code is easier to read and write.



But what are the differences?

windows 10 - Application has been blocked from accessing graphics hardware

When I'm playing CoD Black Ops 3, the game crashes and I get an error that says:


Application "blackops3.exe" has been blocked from accessing graphics hardware

It crashes always when I play it, just after a varying amount of time (depending on the settings and the game mode).


The game freezes and when I open task manager, the game goes black and I can see the error message, but I can still hear the game audio.


I tried to:



  • update the graphics drivers


  • completely lower the settings (1024x768 resolution, 50% resolution scale, FPS limit 30, everything to low or off if possible)


  • uninstall drivers with DDU and (manually) delete all GPU driver related registry entries


  • set TdrDelay to 8 (setting it to higher values just makes the game frozen for longer before getting a black screen); that fixed the "Display driver stopped responding and has recovered" problem I had before this


  • set TdrLevel to 0 (turn off TDR completely), it just completely freezes everything and I have to kill the computer with the power button


  • uninstall and install the GPU through Device Manager


  • reinstall the game


  • reinstall Windows


  • go back to Windows 7


  • increase paging file size


  • BO3 is allowed through the firewall, as well as Steam


  • running in compatibility mode for Windows 7/8


  • running as admin


  • setting process priority to high (it just gets reverted to normal)



But none of the above fixed the issue.
The game worked perfectly for the first three or four weeks when I bought it and then it started giving me the "Display driver stopped responding and has recovered" error. After fixing that, I started getting this error.


I am confused and I tried so many things because the game worked before, I get 50 - 60 FPS easily and most of all, the previous CoD (Advanced Warfare) which has almost identical system requirements doesn't crash at all (even at relatively high settings). With that taken into consideration it seems that my computer is able to run BO3 but it just doesn't feel like doing it.


Why is this happening, and if you know, how do I fix this?


By the way, here is a screenshot of a private match with bots and one from before where guns look like clay (the textures don't load properly), although this doesn't happen anymore (see update 4). I know the second screenshot is bad, but that's the only one I have which shows this issue.


EDIT:


I see no one has any idea how to fix this. But as @user3064419 said, what is blocking the application in the first place? Is there any documentation about this error?


Any help would be appreciated.


UPDATE:


So I thought that this may be caused by corrupt files.


I ran sfc /scannow in cmd and it came up with this:


Windows Resource Protection found corrupt files but was unable to fix some of them.
Details are included in the CBS.log windir\Logs\CBS\CBS.log.
For example C:\Windows\Logs\CBS\CBS.log. Note that logging is currently not
supported in offline servicing scenarios.

The log says that opencl.dll is corrupt. Does this have anything to do with my problem?


UPDATE 2:


So I used DISM.exe to fix the corrupt file and sfc /scannow now reports no errors. But it doesn't seem to make any difference.


UPDATE 3


I think that if I modify the config.ini file, I could achieve something.
You can see the file contents here.
I assume that the most important values are VideoMemory, CorpseCount, MeshQuality, TextureQuality, TextureQualityFX, TextureQualityProbes and TextureQualityBakedSunShadows. I have set all of these to minimum. Also, I saw a value at the end of the file called StreamMinResident. Its description is Reduces memory reservations on systems with low GPU memory. and it can be 0 or 1 (off\on, boolean value). I set it to 1, but it doesn't seem to do much.


UPDATE 4


Something weird has been happening recently. Textures and camos load properly even in public matches (i.e. guns don't look like clay anymore, see above) and I have been able to complete a few campaign missions.


UPDATE 5:


Now I have played the Battlefield 1 open beta and that runs beautifully. By that I mean the resolution is set to 1280x1024, resolution scale to minimum (or just a little bit), all settings to low (except AA which is set to minimum, not off), the game runs at about 45 - 60 FPS and most importantly, doesn't crash at all.


Now I think the key thing here is the resolution scale. The same setting exists in Black Ops 3 where it can be reduced to minimum 50%, however in Battlefield 1, it can be reduced even further. The strange thing is that, in Black Ops 3 I set it to 50% and the game crashes, but in Battlefield 1 when I set it to 50%, it still doesn't crash, only the framerate becomes unbearable.


UPDATE 6:


OK, I'll have to stop numbering the updates. Recently I broke Windows on my computer. When booting, it would refuse to do it and say "Your PC has ran into a problem and has to restart". Then it would restart and do this again in an infinite loop.


I removed and installed Windows 10 again with Bootcamp properly and for the first two days, the game worked perfectly and now it started showing the Display driver has stopped responding and has recovered error. What?! I know I can set TdrDelay to 8 or whatever, but then I'll start getting the error in the title. I haven't done anything with driver updates and things like that.


I've thought of something. Could this be caused by a non-activated copy of Windows?


System:



  • Windows 10 x64


  • Intel Core i5 4th gen @ 2.6 GHz (turbo boost up to 3.3 GHz)


  • Intel Iris 5100 graphics


  • Bootcamped Macbook Pro mid-2014



Note: I am crazy enough to play Black Ops 3 on a Mac (you don't have to tell me)

html - Custom Javascript Alert Popup onclick svg path

I have embedded an svg file into an html file and can view it in all browsers and platforms. I now want to make specific paths within the svg clickable. I have used an onclick event to perform this function, however, I have only been able to get a javascript alert box to popup when the path is clicked. Instead of the default javascript alert, I need a custom popup which I can then include unique custom text and a hyperlink url for each of the paths. Therefore, each path would have its own text and hyperlink.



Does anyone know the best/easiest way to accomplish this?



Excerpts from my code below:









xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:a="http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/"
x="0px" y="0px" width="771.429px" height="986.584px" viewBox="0 0 771.429 986.584" enable-background="new 0 0 771.429 986.584"
xml:space="preserve">










hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...