Situation
function x(){
var f = null; // PROPRIEDADE PRIVADA DE x
this.a = null; // AS INSTANCIAS DE x TERÃO ACESSO A PROPRIEDADE a
x.b = null; // ACESSIVEL COMO x.a
}
function y(){
x.call(this); // HERANCA DE x
this.c = null;
y.d = null;
}
function z(){
this.c = null;
z.d = null;
}
z.prototype = x;
var t = new y;
console.log(t); // Object { a: null, c: null}
console.log(t.__proto__); // Object { constructor : y(), __proto__:Object }
// NÃO ME EXIBE b NEM d
var p = new z;
console.log(p); // Object { c: null}
console.log(p.__proto__); // fuction x()
// NÃO ME EXIBE b, MAS EU TENHO ACESSO p.b
Understanding
-
x.b
is actually accessing the super globalwindow[x][b]
and setting withvalue = null
. -
p.b
has access tob
, as it searches the objectp
, not finding it passes__proto__
which isx
.console.log(typeof x) // function console.log(typeof x.b) // object
In JavaScript, almost everything is an object. All primitive types - with the exception of null and undefined - are treated as objects. They can receive properties (assigned properties of some types are not persistent), and have all the characteristics of objects. MDN
Questions
- Is the above understanding correct?
- Why
t
does not have access tot.b
, just likep
does not have access top.d
? - Why
p
does not have access top.a
, even though__proto__ = x
? -
x.b
is the same as a static property?