JavaScript - Get calling object [duplicate] - javascript

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Javascript how do you find the caller function?
Is there a way to get the value of this from the function which has called the current function?
Look at this:
function TraceMySelf(){
console.log(this);
}
function A(){
TraceMySelf();
console.log(this);
}
var a = new A();
When this code is executed, the console displays first the window object and then the a object. How can I make the code display the a object twice, with only changing line 2? I know that I could apply the function inside A with this, but that isn't what I want.
Is this possible?

I think this is the answer to your question: StackOverflow 280389
However, I think the right answer is "don't do that". I think it runs counter to how JavaScript is designed.
It might also be worth looking at jQuery Proxy for another way of linking function and object.

Related

d3 examples, is chart = { ... } a function or class or object? [duplicate]

This question already has an answer here:
Return value in non-function Javascript code block
(1 answer)
Closed 1 year ago.
In all the d3 examples like this one , the svg appending part of the code is encapsulated in
chart = {
... lines and lines of codes ...
}
I first thought it is a function, since there's a return statement in it. But of all the arrow function I read up in MDN seemed all require an arrow =>.
Is this a object? But object are variables and functions with colons, but this one do not.
I'm confused as to what it is. Could you help me to understand?
The best way to understand the Observable's samples and work with them, is to download the code and run it in a fiddle, sandbox, or any other environment.

Functions that call eachother [duplicate]

This question already has answers here:
Are variables statically or dynamically "scoped" in javascript?
(7 answers)
What is the scope of variables in JavaScript?
(27 answers)
Closed 2 years ago.
I am looking for someone to explain me something since I can't find a clear answer. My question is about functions that call themselves. I saw that it is possible to build a list of functions that are 'chained' together and for example the first function calls the second one then the second one calls another one. My confusion is : Lets say that you have a second function that has a variable let a = 12; if i call that function on the first function, will i have access to that variable or whatever that second function might have inside? How can i pass info to another function? can a function can be dependent on another function in order to complete a task? Thanks in advance guys.
Edit to make it more clear what I mean:
function first(){
second();
// will i have access to whatever there is inside function second since i am calling it here ? or it doesn't work that way?
}
function second(){
let a = 12;
third();
}
function third(){
fourth()....
}

Function excuted before creation [duplicate]

This question already has answers here:
Why can I use a function before it's defined in JavaScript?
(7 answers)
Closed 9 years ago.
I am no JavaScript expert, but I found some code like this
a();
function a(){
alert('a');
}
and I was surprised to find it works (I think something like that will not work in Python). I expected that the function a can't be executed before being created. How does interpreter work and why functions can be called before declaration?
This happens because of variable hoisting.
See this answer for more info
JavaScript 'hoisting'
Some docs about this:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var#var_hoisting
If you type it like this, it won't work:
a();
a = function(){
alert('a');
}
Code that is within functions and objects will be run whenever that
function or object is called. If it is called from code that is
directly in the head or body of the page then its place in the
execution order is effectively the spot where the function or object
is called from the direct code.
See the reference here.
And in our case the function will give the error as you can see the example here.
It's because function a() is declared via Function Declaration syntax, and Function Declaration are executed right after the script is parsed. With other syntax, Function Expression, like this:
var b = function(){
alert('b');
}
it won't work (see example).
More info: http://javascriptweblog.wordpress.com/2010/07/06/function-declarations-vs-function-expressions/

why do we need to pass in window and undefined into this jquery plugin? [duplicate]

This question already has answers here:
What is the purpose of passing-in undefined?
(3 answers)
Closed 8 years ago.
I'm looking at jquery resize plugin and can't understand certain things about how it works:
usually we only pass in Jquery object into jquery plugins, like this:
(function($){
....plugin code....
})(jQuery);
In "resize" plugin there are window and undefined objects being passed in:
(function($,window,undefined){
....plugin code....
})(jQuery,this);
IMHO - window is a global object anyway - why do we need to pass it in? the logic behind passing in undefined object I understand even less. I'm sure there's gotta be some reason for it - but I cannot think of any.
Can someone explain why is it being done?
this is explained very well in this video.
basically, you can set those variables in the self invoking function to ensure they work as expected.
"the asshole effect" undefined = true; -paul irish
furthermore by passing these as arguments they can also be minified.
ie.
(function(A,B,C){
....plugin code....
})(jQuery,this);

Javascript new way to define object properties and functions? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What is the purpose of wrapping whole Javascript files in anonymous functions like “(function(){ … })()”?
I have stumbled upon many times this unusual way of coding, and it seems to be becoming popular.
(function (variable) {
// properties or methods go here
// some more stuff
})(variable)
It's hard for me to even research it, because i don't even know how it's called. i have worked with it with jquery with but i still don't know how it
works.
example:
(function ($) {
...
// code to manipulate the dom
function init() {
.....
}
$(document).ready(function () {
init();
});
})(jQuery);
I've only used it because i was updating some code made by another developer.
Is there any advantage to coding like this? Is there a place where i can read more about it?
If anybody understand my question, it will be nice to see some articles that talk about this, or maybe you have some insight on how to make your own.
Thank You
Ibu
Its called a self invoking anonymous function. Look at this thread for more details about how it works and why its used,
Why do you need to invoke an anonymous function on the same line?

Categories