Javascript Access Modifiers

Asked

Viewed 1,124 times

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 global window[x][b] and setando with value = null.
  • p.b has access to b, because he searches for the object p, not finding passes to the __proto__ that is x.

    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 to t.b, as well as p does not have access to p.d?
  • Why p does not have access to p.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.

  • @bfavaretto also :D

  • you know that the this, is not always the this 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.

  • 1

    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

3 answers

6


Hello,

The above understanding is correct?

Not.

I believe that first of all we must understand the context...

By relating your doubt to "Javascript Access Modifiers" there is a point of reflection, in fact nay there are access modifiers in Javascript, because it is a nested scope language. The famous Closures (there is already a question on the subject here) create the appearance of access modifiers where you can make certain functions available to objects at certain times.

Because t does not have access to t.b, just as p does not have access to p.d?

Because p doesn’t have access to the p.a, even though it may have as proto = x?

Understand:

These nested scopes are based where they were set...

Then when defining a closure with another function, the scope of this closure will be nestled inside of the scope of the function.

function a(){
  function b(){
    }
  }

That means that the access the variables must pass through a scope chain until you find the correct scope where the variable was defined.

Therefore, all the functions that are defined within of the scope of another function, will be at all times closures. Even if they are appointed functions.

Whereas the root scope is the global scope of all functions, all functions are nested within the global scope.

Therefore, all functions in Javascript, are automatically closures.

var x = 1;
function a(){
  x = 2; //Acesso global, logo X recebe 2
  var y = 3;
  function b(){
    y = 4; //Y aninhado dentro da função a() recebe 4
    var x = 5; //Variável local
  }
  b();
}
a();

Soon we have to, X = 2 and Y = Undefined

I may be wrong, but maybe you’re misunderstanding the use of Prototype, which is normally used for simulate the attributes of a simulation of class javascript.

x.b is the same as a statistical property?

There’s no way to relate x.b the other thing if not à a mistake.

  • 2

    Is the crack thing a joke? Because then it would be without a star :P

  • 3

    Yes, it’s a joke. I believe a touch of humor doesn’t hurt. = D

  • 1

    Beauty, I am in favor of humor :) It is that in writing sometimes it is not clear the intention

5

  1. The above understanding is correct?
    R: x.b and var f = null, do not receive anything globally, because they are private attributes of class x, only work within it, as they were not used for anything, they will never be accessed by anything. The same goes for y.d and z.d. To access window globally you would have to do this outside the method function x(){ ... }.

  2. Because t does not have access to t.b, just as p does not have access to p.d?
    R: t is instance of y, has no access to b (attribute of x) and p is instance of z, z has no access to d (attribute of y), even if y calls attributes of x, this do call is scope of x, in this sense, they still belong to element x.

  3. Because p doesn’t have access to the p.a, even though it may have as proto = x?
    R: p is instance of z, there is no way to access (attribute of x), even if z receives an inheritance in the constructor of x, this inheritance will only inherit dynamic attributes (this) of object / class z, and the prototype will only inherit method x, however no attribute of x will go into the scope of p if it does not have a constructor. Because they still belong to object x, but x belongs to p now. but you have access to p.c, because c is a dynamic attribute of z. To do what you want, you need to create a constructor as follows:

    z. prototype = x;
    z.prototype.__proto__ = z;

    or

    z. prototype = x;
    z.prototype.constructor = z;

    or

    z. prototype = new x;

  4. x.b is the same as a statistical property?
    R: b is a static and private attribute of x, not alternating in scope dynamically, like the this what makes.

0

Let’s see if I can help.

The above understanding is correct?

Yes, that is correct.

Because t does not have access to t.b, just as p does not have access to p.d?

Pq x.b is static, and after you instance Coo object, you lose access to static properties.

Because p doesn’t have access to the p.a, even though it may have as proto = x?

Because the syntax wasn’t right, it had to be: z.prototype = new x;

x.b is the same as a statistical property?

Yes, and when you instancia x, you lose access to x.b.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.