What is the behavior of two anonymous functions in an object?

Asked

Viewed 50 times

1

Explain:

StarGate = {};
StarGate.c = {}; 
StarGate.c.m = function(t, e){
  return "teste1";
},function(g){
  console.log('teste2');
}(StarGate.c.m.prototype); 

I have doubts because, what would be the behavior of the second function? It superimposes the first function on the object? It is executed as soon as created, thus it has access to the properties and methods of the object in question?

The excerpt from the real code I have is this(I had not put it previously because I thought it could get big in the question):

StarGate.c.m = function(t, e, a) {
    var o = arguments.length;
    2 === o && (a = 1), 2 === o || 3 === o ? (this.phi = t, this.theta = e, this.rad = a) : (o = t.x * t.x + t.y * t.y, 
    this.rad = Math.sqrt(o + t.z * t.z), this.phi = 0 === t.x && 0 === t.y ? 0 : Math.atan2(t.y, t.x), 
    this.phi < 0 && (this.phi += StarGate.b.p), o = Math.sqrt(o), this.theta = 0 === t.z && 0 === o ? 0 : Math.atan2(t.z, o));
}, function(g) {
    g.G = function() {
        var t = Math.cos(this.theta);
        e = this.rad;
        return new StarGate.c.k(e * g * Math.cos(this.phi), e * g * Math.sin(this.phi), e * Math.sin(this.theta));
    };

    g.K = function() {
        return new StarGate.c.m(this.rad, this.phi, this.theta);
    };
}(StarGate.c.m.prototype); 

Grateful

1 answer

3

I highly recommend writing code that way. Because the comma operator generates hard to read code, and because passing prototypes as arguments of functions will generate complexity and consequently unexpected behaviors.

The comma operator in this case it executes the first command it will assign to .c.m the value of a function; and how this returns something with Boolean value true, which causes it to execute the other command of this sequence of two commands separated by comma.

Having said that what is happening there, in practice it is:

StarGate = {};
StarGate.c = {}; 
StarGate.c.m = function(t, e){
  return "teste1";
};
(function(g){
  console.log('teste2');
})(StarGate.c.m.prototype);

That is, everything "normal" until the IIFE. This function has in the variable g a function reference that returns teste1 in .contructor and you can use it to invoke that function:

StarGate = {};
StarGate.c = {};
StarGate.c.m = function(t, e) {
    return "teste1";
  },
  function(g) {
    console.log('teste2', String(g.constructor()));

    console.log('A descrição da função:', String(g.constructor));

    console.log('O retorno de invocar a função: ', g.constructor());
  }(StarGate.c.m.prototype);

Regarding what you can access and the execution context, I leave an example. But a function assigned to an object property can never access the upper levels if it is not explicitly called with another context.

StarGate = {};
StarGate.c = {};
StarGate.c.m = function(t, e) {
  console.log(this);
};

(function(g) {
console.log(g);
  g.constructor(); // protótipo
  g.constructor.call(StarGate); // o objeto base
  StarGate.c.m(); // o objeto aninhado onde m é declarado
})(StarGate.c.m.prototype);

// Abre isto na consola do browser

  • But are the methods and taxes of the object accessible by the second anonymous function? What I put in the question is only part of the code I have, because I believe it would be great for here.

  • You do not recommend this form, which would be a more appropriate form of this code?

  • @Gilsonjosé put together 3 examples of ways to call the function with different execution contexts. Regarding another way of doing it depends on the application. How it is is complex and difficult to read. Separating into IIFE as I put together in response would be better.

  • I updated the body of the question to better understand it. The second anonymous function, I understand, is accessing the attributes of the Stargate object. c certain?

  • @Gilsonjosé putting together that whole code explodes the question :) Analyzing that code and giving you a safe answer takes more time than I have available and I doubt the question is interesting to other people because it gets too specific. The second anonymous function can access Stargate.c because it was declared before the function runs. But for that you have to have Stargate.c within the function, going by this do not get there Javascript does not allow.

Browser other questions tagged

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