I want to call a function with an argument and store the value so that next time I call that function without arguments it uses the last argument that was set. Is this possible with JavaScript?
Edit: Here's more info on what I'm trying to achieve...
var track = 0;
$.getJSON('songsMetadata.json', function(data){
appendData(data);
});
player.bind("ended", function(){
track++
appendData();
});
function appendData(data){
/* Here I want to populate
the inside 'data' argument
only once (in the callback of
getJSON) and use that same
data in the 'player.bind'
callback */
//Some code to append data[track] to an HTML list
}
I want to call a function with an argument and store the value so that next time I call that function without arguments it uses the last argument that was set. Is this possible with JavaScript?
Edit: Here's more info on what I'm trying to achieve...
var track = 0;
$.getJSON('songsMetadata.json', function(data){
appendData(data);
});
player.bind("ended", function(){
track++
appendData();
});
function appendData(data){
/* Here I want to populate
the inside 'data' argument
only once (in the callback of
getJSON) and use that same
data in the 'player.bind'
callback */
//Some code to append data[track] to an HTML list
}
Share
Improve this question
edited Jun 10, 2017 at 21:40
medicengonzo
asked Jun 10, 2017 at 19:47
medicengonzomedicengonzo
5192 gold badges10 silver badges23 bronze badges
5
- yeah, sure it can be done but if you could please give me a bit more information, i could write you an example. what will the argument be? – Molik Miah Commented Jun 10, 2017 at 19:48
- Possible duplicate of Javascript: Creating a function with state – Andreas Commented Jun 10, 2017 at 20:02
- You may be interested in iterators and generators – BoltKey Commented Jun 10, 2017 at 20:06
- @MolikMiah the argument will be an array that I get from an Ajax call to a JSON file. The array contains mp3s metadata from an HTML 5 playlist that I coded. What I want to do is append this data gradually into the HTML (showing the metadata as the playlist plays each song). I'm still a little bit confused about asynchronous code – medicengonzo Commented Jun 10, 2017 at 20:41
-
You can use either of the existing answers to acplish this. Based on my answer for example - you just have to wrap
appendData
in the wrapper function.var cachedAppend = cacheFn(appendData);
then usecacheAppend
in all places you would useappendData
. This normalizesappendData
so it always gets called with some argument, either the one being passed, or the last argument to have been passed (as you specified in your question). – Damon Commented Jun 10, 2017 at 23:24
2 Answers
Reset to default 4You need to keep a reference to the last acceptable argument in the enclosing scope. For example:
var ref;
function foo (arg) {
if (!arg) { // or arg === undefined if you want to be safe
arg = ref;
} else {
ref = arg;
}
// actual function behavior here
console.log(arg);
}
foo(); // undefined
foo(2); // 2
foo(); // 2
foo(3); // 3
foo(); // 3
If you want to repeat this behavior, you might want to consider writing a wrapper function to cache an accepted function's argument. For example:
function cacheFn (fn) {
var ref;
return function (arg) {
if (!arg) {
arg = ref;
} else {
ref = arg;
}
return fn(arg);
}
}
function foo (arg) {
console.log(arg);
}
var cachedFoo = cacheFn(foo);
cachedFoo(2);
cachedFoo();
cachedFoo(3);
cachedFoo();
In a more general way:
function enclose(func) {
let args = [];
return function (...passed) {
if(passed.length) args = passed;
func(...args);
};
}
Usecase:
const log = enclose(console.log.bind(console));
log(5,1);
log();
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745387635a4625510.html
评论列表(0条)