Trouble understanding Javascript closure example

Trouble understanding Javascript closure example

By : krishna priyanka
Date : November 22 2020, 03:01 PM
Hope this helps I understand most of what closures are, but then this example got me quite confused. I will post two code examples: the first one will be the original quote where they explain the workings of closure and the second will be code where I added something, but it did not give me the results I expected. ,
Why does obj.someVar++ change the result in the last two lines?
code :

Share : facebook icon twitter icon
Understanding closure in Javascript

Understanding closure in Javascript

By : user2362286
Date : March 29 2020, 07:55 AM
will help you 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.
code :
// 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); };
Need Help Understanding Javascript Closure

Need Help Understanding Javascript Closure

By : iomz
Date : March 29 2020, 07:55 AM
it should still fix some issue Let's analyze this step by step.
The first call is to mystery with an argument of 3.
code :
secret = 4;
input = 5;
function mystery4(bonus) {
    return param(6) + bonus
function mystery2 ( multiplier ) { 
    multiplier *= input;
    return secret * multiplier;
Trouble understanding Javascript nested function/closure

Trouble understanding Javascript nested function/closure

By : John Yeiser
Date : March 29 2020, 07:55 AM
it should still fix some issue I'm attempting to port the following JavaScript code to ruby: https://github.com/iguigova/snippets_js/blob/master/pokerIn4Hours/pokerIn4Hours.js
code :
var kickers = function(idx){
    var xyz;//below function in return statement can access this and argument idx
    return function(...) {//this ensures that your kickers is infact a function
}();//invoke this function instantly due to () and assign the output to kickers
/* now  idx and notplayed is global- but its meant to be used only by kicker
 * we cant move it inside the function definition as it will behave as local variable to the function 
 * and hence will be freed up once executed and you cannot maintain the state of idx and notplayed
var idx = -15;
var notplayed = Math.max(input.length - 1/*player input*/ - 5, 0);
var kickers = function(all, cardinality, rank) {            
    return(all || 0) + (((cardinality == 1) && (notplayed-- <= 0)) ? rank * Math.pow(10, ++idx) : 0);
Trouble understanding closure details - setTimeout within for loop

Trouble understanding closure details - setTimeout within for loop

By : Mika
Date : March 29 2020, 07:55 AM
will help you This has nothing to do with the mechanics of closures. This is 100% caused by how functions work.
Now, let's tease apart the two different functions. For clarity I'm going to completely remove the for loop:
code :
var i = 1; // We simply hardcode `i` for this demo.

function a (x) {
    })(i); // immediately call this function

    // Yes, this space with no code at all at the end of
    // this function is the MOST IMPORTANT part of the code.
    // This absence of code represents "return undefined".
    // All functions that return nothing returns undefined.

setTimeout(a(i),i * 1000);
var i = 1;

function b (x) {
  return (function () {
  }) // <--- note you are not calling the inner function at all

setTimeout(b(i), i * 1000);
Having trouble understanding why my Closure wasn't working

Having trouble understanding why my Closure wasn't working

By : zaman kafil
Date : September 21 2020, 10:00 PM
it should still fix some issue Without assigning it to a variable, or returning it, it is not a closure.
This way you have function declaration, within another function or method in this case.
Related Posts Related Posts :
  • How to get text from selected value in a dropdownlist which is js based
  • window is not defined angular universal third library
  • Angularjs ng-repeat stylization depending on previous value
  • Trying to implement Fittext.js
  • Calculate number of match in array Lodash
  • Jquery Smooth Scroll Using Offset.top
  • How to extract data to React state from CSV file using Papa Parse?
  • How to add unique links to google maps markers
  • How to use if condition in a tool bar in java script
  • Ajax filter in django not showing in HTML
  • data collection with Javascript
  • Rotate image on lightbox2 load
  • Prevent body from scrolling when a Pop-Up is open
  • How to copy files that do not need to be compiled in Gulp?
  • Array not assigned to variable? How does this work and what exactly is it doing?
  • Sorting associative array of objects in javascript
  • Changing Icon in Sap.m.tree having CustomTreeItem
  • Merge two array of objects based on a key
  • javascript in css not working
  • Passing only clicked element to onClick function - reactjs
  • React boilerplate doesn't load js files in the index.html
  • is Child service inside child component visible in the Parent component?
  • Check if data attribute value equals a string
  • How to get value of child tag of a button tag
  • How to access subjects of selected mails in Apple Mail using JavaScript?
  • How to get all dynamically set inline-style CSS in jQuery?
  • Error: Module "html" does not provide a view engine (Express)
  • Random Image in <Div> from array
  • Slider with touch function
  • ReactJS Component Architecture Problems / Nested Components or Single Component Manager
  • Javascript: Caching within Closure doesn't work
  • HTM5 Canvas Drawing App: How Do I Select The Color?
  • Assigning Events using HTML DOM
  • html5 getUserMedia() portrait mode
  • How to avoid 'headers already sent' within Promise chain?
  • Get a result from a react native app integrated into an existing android app
  • Why does the value of input field return undefined
  • How to get return object in jquery uploadFile method
  • Javascript string with arguments to single quotes
  • Understanding JS functions syntax (applies to other languages as well, I'm sure)
  • AngularJS Single Page App: Reference Error, ____ is not defined
  • whatwg-fetch new typescript 2.5.3
  • React.js - Array.unshift() not updating array on front-end
  • JavaScript localeCompare don't sort well [ "aaa-1.0", "aaa-1.1", "aaa-1.01", "aaa-11&
  • Mongoose handle validation error in a promise call and Express middleware
  • Building tree array of objects from flat array of objects
  • How to add a progress bar to file upload using javascript and ajax call?
  • JavaScript: is it permitted to override a standard built-in object?
  • Change the current page of an AJAX datatables
  • Array element is undefined even after pushing elements into it
  • onmousemove event not working
  • Chrome-Extension/JavaScript How to make all links on page have no effect?
  • Websocket issue Internet Explorer
  • sessionStorage value becomes null in other tab in AngularJS
  • Split URL from position x to the end in Javascript
  • How to set value of input field with NgIf in Angular 4
  • invalidStateError for second time screenshare on Chrome
  • How do you pass a value to the variable in a xhr.setRequestHeader?
  • I cant start nodemon on my windows 10
  • Nested if else jumps out from the main if else in javascript
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org