13
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
in fact is accessing the super globalwindow[x][b]
and setando withvalue = null
.p.b
has access tob
, because he searches for the objectp
, not finding passes to the__proto__
that 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 may receive properties (assigned properties of some types are not persistent), and have all the characteristics of objects. MDN
Doubts
- The above understanding is correct?
- Why
t
does not have access tot.b
, as well asp
does not have access top.d
? - Why
p
does not have access top.a
, even though it may__proto__ = x
? x.b
is the same as a statistical property?
@mgibsonbr, if you can help, I’ve read many of your answers, always very good.
– Guilherme Lautert
@bfavaretto also :D
– Guilherme Lautert
you know that the
this
, is not always thethis
that you are using, it varies according to the scope... it is like a pointer..., another detail, that you have to consider, is that one does not see the other until it exists.– Ivan Ferrer
Guilherme, you know that neither I nor Gibson were notified of your comments above, right? I saw it by chance. See http://meta.pt.stackoverflow.com/q/943
– bfavaretto