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
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.
– Gilson José
You do not recommend this form, which would be a more appropriate form of this code?
– Gilson José
@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.
– Sergio
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?
– Gilson José
@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 haveStargate.c
within the function, going bythis
do not get there Javascript does not allow.– Sergio