function A() {
this.a = 'this is a';
var b = 'this is b';
}
function B() {
var self = this;
this.c = 'this is c';
var d = 'this is d';
// a: undefined, b: undefined, c: this is c, d: this is d
$("#txt1").text('a: ' + A.a + ', b: ' + b + ', c: ' + this.c + ', d: ' + d);
C();
function C() {
// this.c is not defined here
// a: undefined, b: undefined, c: this is c, d: this is d
$("#txt2").text('a: ' + A.a + ', b: ' + b + ', c: ' + self.c + ', d: ' + d);
}
}
B.prototype = new A();
var b = new B();
Is it possible for class B and inner function C to get variable a and b?
Fiddle file is here: http://jsfiddle.net/vTUqc/5/
You can get
ainB, usingthis.a, since the prototype ofBis an instance ofA. You can also getainC, usingself.a:You can’t get
bdirectly on the other hand. If you want to access it, you could use a function that returns its value:Now
bis accessible to instances ofAvia(new A()).returnb()