Why does the following not work? According to everything I've read it looks like it should work?
a = {
  test: "hello",
  test2: this.test
};
I do a console.log(a) and I get test2: undefined.
Why does the following not work? According to everything I've read it looks like it should work?
a = {
  test: "hello",
  test2: this.test
};
I do a console.log(a) and I get test2: undefined.
 
    
     
    
    In this example, this refers to the value of this relative to the statement a = ..., which is probably window (if you're running this in the browser, and if this is the entirety of the code).
If you wrote a constructor:
var A = function() {
    this.test = "hello";
    this.test2 = this.test;
};
var a = new A();
... the value of a.test2 would be what you'd expect.
 
    
    Because your this refers to the window and there is no global variable/object named test, so window.test is undefined
