JavaScript Access Modifiers

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 is actually accessing the super global window[x][b] and setting with value = null .
  • p.b has access to b , as it searches the object p , not finding it passes __proto__ which 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 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 to t.b , just like p does not have access to p.d ?
  • Why p does not have access to p.a , even though __proto__ = x ?
  • x.b is the same as a static property?
asked by anonymous 24.11.2015 / 15:33

3 answers

6

Hello,

  

Is the above understanding correct?

No.

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

When you relate your question to " JavaScript Access Modifiers ", there is a point of reflection, in fact no JavaScript access modifiers exist, since 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.

  

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

     

Why p does not have access to p, even though proto = x?

Understand:

These nested scopes are based on where they were defined ...

So when defining a closure with another function, the scope of this closure will be nested within scope of the function.

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

This means that access to variables must go through a chain of scopes until it finds the correct scope where the variable was defined .

So all functions that are within from the scope of other role will be always closures. Same if they are named functions.

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

So 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();

So we have to X = 2 and Y = undefined

I may be wrong, but perhaps you are misunderstanding the use of Prototype , which is typically used to simulate the attributes of a simulation of class in JavaScript.

  

x.b is the same as a static property?

There is no way to relate xb to something else if not to an error.

    
03.12.2015 / 14:15
5
  • R: xb and var f = null, do not receive anything globally, since they are private attributes of class x, they only work inside it, like were not used for anything, they will never be accessed for anything. The same goes for y.d and z.d. To access window globally you would have to do this outside the function x(){ ... } method.

  • Why does t not have access to tb, just as p does not have access to pd? A: t is an instance of y, does not have access to b x) and z is an instance of z, z does not have access to d (attribute of y), even if y calls the attributes of x, this of call is of the scope of x, in this sense, they still belong to element x .

  • Why p does not have access to pa, even though having proto = x? R: p is an instance of z, attribute of x), even if z receives an inheritance in the constructor of x, this inheritance will only inherit dynamic attributes (this) from the object / class z, and the prototype will only inherit the x method, but no attribute of x will go into the scope of p if it does not have a constructor. For 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;

  • xb is the same as a static property?
    R: b is a static and private attribute of x, does not dynamically change in scope, as this does.
  • 26.11.2015 / 22:08
    0

    Let's see if I can help.

      

    Is the above understanding correct?

    Yes, that's correct.

      

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

    Because x.b is static, and after you instantiate the object, you lose access to the static properties.

      

    Why p does not have access to p, even though proto = x?

    Because the syntax was not right, it had to be: z.prototype = new x;

      

    x.b is the same as a static property?

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

        
    26.11.2015 / 19:32