How to call a specific function from inside a subfunction in javascript? - javascript

How do I call function "a" from "a.v" without a direct reference "a()"
function a(){
alert("1");
this.v=function(){alert("hi")};
}

One way I can think of doing this without repeating a:
function a(){
let indirect = arguments.callee;
alert("1");
indirect.v=function(){ indirect(); };
}
a();
a.v();
But this does not work under strict mode and you must call a before calling a.v.
You could also do:
function a(){
alert('1');
this.v = () => this();
}
a.v = a;
a.v();
This way you don't call a() and it also works under strict mode.

Set its v property outside the function:
function a() {
alert("1");
}
a.v = function() {
a()
alert("hi")
};
a.v()

Related

Javascript function scope and parameter

Here is the code:
function b() {
console.log(x);
};
function a() {
var x= 1;
b();
}
a();
//the output is : x is not defined!
Can anybody help explain why it will ouput undefined? I thought it would output 1. Why function b() can't get the variable x?
You're confusing closures with actual invoking of a function. If you had function b inside function a, then you can access x like so.
function a() {
var x= 1;
function b() {
console.log(x); // print out 1
};
b();
}
You must pass x variable as parameter:
function b(x) {
console.log(x);
};
function a() {
var x= 1;
b(x);
}
a();
If a var is define inside a function, the var can only be use within the function. If a var is define outside the function, then it can be use anywhere. Your var x =1 is defined inside function a(), so it can only be use inside function a(). To solve your code, you can simply move var x= 1 outside function a(). Here's the code:
function b() {
console.log(x);
}
var x= 1;
function a() {
b();
}
Or, I could suggest you use this instead. It's much shorter:
var x= 1
function b() {
console.log(x);
}

How to read value from javascript function?

If I do this in javascript
var A = function() {
alert(this.foo);
};
A["foo"] = "bar";
A();
I expect to alert bar but I get undefined, does anyone know how I can make this work?
Thanks
The value of this is the object upon which the method was called (unless you make use of the new operator or something like call or bind). Since you didn't call the function as a method then it is the default object (window in a browser) unless you are in strict mode.
The only reference you have to the function in scope is A, so you can only access it via alert(A.foo).
If you had used a named function expression:
var A = function myFunction () {
then you would have had the variable myFunction locally scoped to that function which you could use instead of A.
this refers to the "parent" object of the function, not the function itself. There's no parent in the expression A(). To "make that work", you'd have to explicitly pass A as the this value:
A.call(A);
The way it's usually meant to work is this:
var A = {
alert: function () {
alert(this.foo);
}
};
A.foo = 'bar';
A.alert();
The A from A.alert() is used as this value inside alert().
var A = function() {
alert(this.foo);
};
A["foo"] = "bar";
A.call(A);
or
var A = function() {
};
A.prototype.alert = function () {
alert(this.foo);
}
var a = new A();
a["foo"] = "bar";
a.alert();

How to call nested function in javascript?

I have two function like this..
function a() {
function b() {
alert('reached');
}
}
how i call function b() from outside of function a()..
In general, you can't. The point of defining a function inside another function is to scope it to that function.
The only way for b to be accessible outside of a is if (when you call a) you do something inside a to make it available outside of a.
This is usually done to create a closure, so that b could access variables from a while not making those variables public. The normal way to do this is to return b.
function a() {
var c = 0;
function b() {
alert(c++);
}
return b;
}
var d = a();
d();
d();
d();
var e = a();
e();
e();
e();
You can rearange your code to this
function a() {
this.b = function() {
alert('reached');
}
}
now instantiate it and run:
c = new a();
c.b();
You will need to return the inner function call.
function a() {
function b() {
alert('reached');
}
return b();
}
And then you can call this function simply by calling function a like this:
a();
The output will be the required alert.

Accessing values of function object from within it's nested function

Lets say we have function test():
function test(){
a();
this.b = function(){
alert(1);
}
function a(){
this.b();
}
}
var t = new test();
This code will throw TypeError: this.b is not a function
The question is, how can we correctly access b() from within a()?
Change the order:
function test(){
var me = this;
this.b = function(){
alert(1);
}
a();
function a(){
me.b();
}
}
You can't call this.b() before you've assigned the variable. And you need to use a local variable to capture the value of this in the closure.
function test(){
var me = this;
this.b = function () {
console.log(1);
}
function a() {
console.log('calling me.b()');
me.b();
}
a();
}
var t = new test();
console.log('calling t.b()');
t.b();

Ways to declare functions [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
JavaScript: var functionName = function() {} vs function functionName() {}
What is the difference between those two functions
function a()
{
b=2;
alert(b);
}
a();
and this function
var a=function()
{
b=2
alert(b);
}
a();
what is the main difference
The main difference is that when you declare function:
function a(){
// something...
}
it becomes accessible in the same scope even before the place in the code where it is declared.
But when you assign anonymous function to a variable:
var a = function(){
// something...
};
it is not available before the assignement.
When the functions are created
It is a result of when the function is actually created. In first case it is created when the code is compiled, while in the second case the function is created when the interpreter reaches the line of assignment.
Test code
You can see the difference I mentioned above by executing the following code (jsfiddle):
try {
a();
} catch(e) {
alert('problem calling function a(): ' + e);
};
try {
b();
} catch(e) {
alert('problem calling function b(): ' + e);
};
​function a(){
alert('function a() called');
};
var b = function(){
alert('function b() called');
};​
You will see (as in mentioned jsfiddle), that a() function is called properly even before the actual declaration, but b() is not available before the assignment.
The only real difference is that the second function has no name, and that the function a() {} one is hoisted.
The difference is that function a() is defined at parse-time for a script block while var a=function() is defined at run-time.
<script type="text/javascript">
// No errors occured;
function a();
function a(){
console.log("Success");
}
</script>
<script type="text/javascript">
// An error will occured;
a();
var a = function (){
console.log("Success");
}
</script>

Categories