-1
var Global = {
    alpha : 1,
    color : 'rgba(0, 0, 0, '+this.alpha+')'
}
console.log(Global.color);  

this code is something wrong. In this code, How can i use var alpha in Global.color. Please help me.

ASTOMUSIC
  • 119
  • 1
  • 3

2 Answers2

1

Global.alpha? Global is a horrible variable name - whose global is it and js is nitpicked for the global scope to begin with.

this.alpha is looking at the current context (which is not going to be Global) and access It's alpha.

var Global = {
    alpha : 1,
    getColor : function ()
    {
        return 'rgba(0, 0, 0, ' + this.alpha + ')';
    }
};

console.log(Global.getColor());
Nate-Wilkins
  • 5,364
  • 4
  • 46
  • 61
  • It's fine, and because of the `var` keyword it's a local variable. Probably in some canvas-related code. – Bergi Aug 21 '13 at 14:08
  • @Bergi If this was only in a script tag wouldn't Global be appended to the global scope? – Nate-Wilkins Aug 21 '13 at 14:14
  • Of course it would, but it hardly is. The surrounding scope is irrelevant to the question, so it was omitted. – Bergi Aug 21 '13 at 14:30
0

I'd do the following:

var Global = { alpha: 1 };
Global.color = 'rgba(0, 0, 0, ' + Global.alpha + ')';
console.log(Global.color);
Étienne Miret
  • 6,448
  • 5
  • 24
  • 36