codingdir logo sitemap sitemap |
Home
PHP
C#
C++
ANDROID
JAVA
JAVASCRIPT
PYTHON

Binding closure variables in Javascript


By : , Category : javascript

Based on Felix King's answer, it cannot be done. :( Too bad. I just wanted to store calls and executed later for some kind of persistence level. This way an interaction can be made even if the computer restarts or anything... It seems the only way is to not use the embedder parameters from the 'closure', huh? Thanks very much anyway!

ReLated :

The function logs the value of j.

The value of j is the value passed as an argument to the outer anonymous function.

That is specified when that function is called (immediately) as the value of i.

i only has one value at a time.

It probably depends a little on which should be preferred. I tend to use the latter (though actually I prefer the former, but some 3rd party libraries we're using limit that). I think the important thing on picking a style is being consistent.

A note for the prototype.bind is that it's not supported by IE8 and below which might cause you a problem.

I think performance wise, I'd expect bind to be a little bit slower as you're calling an extra function in there, but it'll probably depend on browser optimizations. I'll try putting together a jsperf example when their site comes back up to answer that part of the question though.

update

Seems JSPerf isn't going to be up anytime soon. Here's an snippet that I've put together that shows the closure was quicker (assuming I've done it correct). The closure was slightly more than 7x faster. If you run with the console open you'll see the timings.

var limit = 100000;

var a = {
   val: 0,
   do: function(i) { val = i; /* Actually do some work to ensure doesn't get optimised out */ }  
};

var b = {
   myFunc: function(callback) { callback(); /* Function that's going to change this keyword */}   
};



var start = +new Date();

   for(var i = 0; i < limit; i++) {
     b.myFunc(function() {
        this.do(i);
     }.bind(a));
   };

var end =  +new Date();
var diff = end - start;
console.log("bind took " + diff + " ms");

var start = +new Date();

   for(var i = 0; i < limit; i++) {
     var that = a;
     b.myFunc(function() {
        that.do(i);
     });
   };

var end =  +new Date();
var diff = end - start;

console.log("closured took " + diff + " ms");

There's no comprehensive way to get a list of all variables in scope. You could enumerate over the this object, but that will still only give you a list of the enumerable objects on this, and even at that there will still be things like function arguments that aren't on this.

So no, this cannot be done. Also check out this similar question.

An inner anonymous function will have the same scope as it's containing function.

You may consider passing-in zcls, or defining it within the function you are passing to nv.addGraph

MDN explains this really well: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions_and_function_scope

You could define the function argument, then pass it by name. For example:

var fcnArg = function() { ... }

nv.addGraph(fcnArg);

Another good discussion on this available here: Passing a function as an argument in a javascript function

Comments


Message :
Login to Add Your Comments .
How to disable registered OpenCL platforms on Windows?
Is Observable broken in Angular 2 Beta 3?
Cross-thread operation not valid when using Invoke
How to pass an IEnumerable or queryable list of properties from Controller to View
Finding numbers after a certain keyword using Python
Pocketsphinx recognizes random phrases in a silence
Passing non-thread-safe objects through thread-safe containers
React scroll nav
BizTalk WCF-BasicHttp Adapter does not allow Empty string for Service Certificate Props
Why property ''cause" of Exception is repeating forever?
Privacy Policy 2017 © codingdir.com All Rights Reserved .