I have a code as follows:
function Cell(center) {
this.center_cell = center;
calc_neighbours = function() {
var points = this.center_cell;
console.log(points); // displays undefined
};
this.get_neighbours = function() {
return calc_neighbours();
};
}
var c_points = new Array(8,2);
var cell = new Cell(c_points);
cell.get_neighbours();
With above code placed, the function cell.get_neighbours() displays undefined.
Now, If I make a slight change and have the following listed code, then function displays the values. Why is this happening is this because of function scope or variable scope inside javascript's object property.
Here is the code that displays the value:
function Cell(center) {
this.center_cell = center;
this.calc_neighbours = function() {
var points = this.center_cell;
console.log(points); // displays undefined
};
this.get_neighbours = function() {
return this.calc_neighbours();
};
}
I haven't made any changes to the function usuage. i.e.
var c_points = new Array(8,2);
var cell = new Cell(c_points);
cell.get_neighbours();