I'm not being able to access private variables (or arguments) from a prototyped functions. Take this code:
function My_controller(url) {
this.list = [1, 2, 3];
}
My_controller.prototype.order_list = function() {
console.log("url: ", url);
this.list.push(this.list.splice(0, 1)[0]);
console.log(this.list);
}
var ct = new My_controller("http://");
ct.order_list();
It works if the function is defined inside de object:
function My_controller(url) {
this.list = [1, 2, 3];
this.order_list = function() {
console.log("url: ", url);
this.list.push(this.list.splice(0, 1)[0]);
console.log(this.list);
}
}
var ct = new My_controller("http://");
ct.order_list();
Meanwhile this is not the best choice for optimizing the code, is it? So I was wondering why is it not possible to get that variable and how to solve this?
I know one choice would be to save it inside this like:
function My_controller(url) {
this.list = [1, 2, 3];
this.url = url;
}
And then accessing this.url
, instead of url
.
Can you think on a better way to do this?