0

I am learning Javascript and I am a C++ programmer. I have tried creating an object with a constructor with object.create and here is the result:

var PlayGround ={
    initGrid : function(N) {
        this.N=N;

        this.grid = new Array(N);
        for (var i = 0; i < N; i++) {
            this.grid[i] = new Array(N);
            for (var j = 0; j < N; j++) {
                this.grid[i][j] = false;
            }


        }
        return true;

}
};

var PlayGround_property = {
    N:{
        value: 100,
        writable:true
    },
    grid:{
        value:null,
        writable:true
    }

}

var board= Object.create(PlayGround, PlayGround_property);

It works as I want: the object board contains the object grid, and now I can use the set and get keyword to define the behaviour of the = and () operator. Anyway I have read around the web that the

this

keyword in Javascript is not safe and I want to be sure that it is referring always to the board object and not to the global window object. Is there a way or I am overthinking? Other question, are there other ways to write object with a constructor (and maybe other members) in Javascript?

BiA
  • 364
  • 2
  • 6
  • 23
  • When you are inside an object, the `this` keyword always refers to the object and not the window. However, nested inside these objects could be objects that also use this to refer to themselves. You can't go back up the scope, so you'll have to declare a `var self = this;` Otherwise you can be pretty sure `this` refers to the object you are in. – somethinghere Oct 24 '14 at 10:05
  • @somethinghere—"*When you are inside an object, the this keyword always refers to the objec*" is not correct. The *this* keyword is a feature of *functions*, and is set by how the function is called. – RobG Oct 24 '14 at 10:20
  • Where did you get the idea that "*the `this` keyword in Javascript is not safe*"? It is entirely under your control. – RobG Oct 24 '14 at 10:22
  • @RobG Yes, the `this` keyword only applies to functions' scope, but it does refer to the object, otherwise the following would't work: `var a = {b : function(){ alert(this.c); }, c : "log"}; a.b();` – somethinghere Oct 24 '14 at 10:24
  • @RobG from http://javascript.crockford.com/prototypal.html . I have to admit that I do not understood everything about that article. So my could be just an impression. – BiA Oct 24 '14 at 10:25
  • http://stackoverflow.com/questions/80084/in-javascript-why-is-the-this-operator-inconsistent/80127#80127 this answer is the most comprensible for me – BiA Oct 24 '14 at 10:26
  • @somethinghere—how about `var x = a.b; x()` or `a.b.bind(window); a.b()` or `element.onclick = a.b` or … ;-) – RobG Oct 24 '14 at 10:28
  • @RobG Thats what they invented the word _'goshdarnit'_ for. :) – somethinghere Oct 24 '14 at 10:30
  • @BiA—that answer is correct within a limited context, it's incomplete and doesn't fully cover the topic. Try [*MDN this*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this). – RobG Oct 24 '14 at 10:33

1 Answers1

1

I want to be sure that [this] is referring always to the board object

A function's this is set either by how you call the function, or bind. So just make sure you call methods the right way. If you always call functions as methods of board, then this within the methods will always reference board.

If you are only going to have one instance of board, there doesn't seem much point in using a constructor. If you have multiple instances of board, then you want this to reference the particular instance that called the method so you don't want to fix this using bind.

Crockford just doesn't like the use of new, so encouraged Object.create, it fits his idea of how inheritance should work.

Your pattern could be rewritten to use a constructor something like:

function PlayGround (N) {
    this.N = N;
    this.grid = []; // Use array literal, it's less to type

    for (var i = 0; i < N; i++) {
        this.grid[i] = [];

        for (var j = 0; j < N; j++) {
            this.grid[i][j] = false; // Not sure why you bother with this
        }
    }
}

var board = new Playground(100);

I'm not exactly sure what you're doing, but that should be close. Note that javascipt is loosely typed, so only initialise variables and properties if you have something useful to assign. Variables are created with a value of undefined, Array properties are only created if you actually assign something to them, creating an array with length N does not create any indexes, e.g.

var arr = new Array(10);
console.log(arr.length); // 10
console.log(arr.hasOwnProperty(0)); // false
RobG
  • 142,382
  • 31
  • 172
  • 209