Javascript - callback with objects -
i have problem callback function. want write function, can iterate object (i want use callback method), it's not working , don't know wrong this.
i'll glad help.
services = [ { name: "a", }, { name: "b" } ] function service (data) { this.name = data.name } function getdata (i) { sample = new service(services[i]) console.log(sample) } getdata(0) /* function work*/ function getall(index, count, callback) { service = new service(services[index]); console.log(service) if (index < count) { callback(index + 1, count, getall) } } getall (0, services.length, getall) /* function not working */
the error due calling services[2] doesn't exists. getall function below solves problem
function getall(index, count, callback) { if (index < count) { service = new service(services[index]); console.log(service) callback(index + 1, count, getall) } }
Comments
Post a Comment