Quick question: How do I get a pointer to a pre-declared function in javascript.
Say in my .js file I have
function a() {
var bref = `a pointer/ref to b() here`
}
function b(param) {
...
}
I want bref to be simply a pointer to the function b and not just the result of a call to b()
Edit: Forgot to mention: function b takes in one parameter i.e b(param)
Have tried bref = b
Just use the function name for assignment.
To call the function you can use
call
or
apply
methods.
e.g:
function a() {
var bref = b;
.
.
b.call(null, "Something")
}
function b(src) {
...
alert(src);
}
Working example #: http://jsfiddle.net/UasYH/1/
You can simply:
function a() {
var bref = b;
...
x = bref("cake");
}
function b() {
...
}
Related
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()
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);
}
I have created nested function in one script file A.js, like below:
function A(){
function B(){}
}
If I want to call function B() in another script file C.js, what should I do?
What you want to do seems to be to create a function closure of B using the variables within A(). You can then access this closure B if you return B after you call A(). Then, in C.js, you can access B by calling A and using the return value:
A.js:
function A([parameters]) {
[variables]
function B() { [function body] }
//Return B from A:
return B;
}
C.js:
//Get B:
var bFunction = A([parameters]):
//Call bFunction:
bFunction();
You have to return the nested function:
function A(){
return function B(){}
}
Then In C.js
var funB = A();
funB();
or
A()();
You have to return the nested function B() from the parent function A().
Your code
function A(){
function B(){}
}
Updated Code with return statement
function A(){
return function B(){ //return added
}
}
You can access the child function by adding an additional () Parentheses to the function call like the below example.
A()();
I don't understand a part of the accpeted answer of this OP:
Javascript function scoping and hoisting
Writer says:
"
Also, in this instance,
function a() {}
behaved the same as
var a = function () {};
".
What I know is that function expression is different than function declaration, at least for hoisting. Why are they similar in this case?
Why are they similar in this case?
Because var is hoisted (but not set), like a function declaration is hoisted, meaning that there is an a in the local scope before a = 10; is evaluated, so the global a never gets modified - the identifier lookup finds the local a first so sets that
Related parts of the other question
var a = 1;
function b() {
a = 10;
return;
function a() {}
}
b();
alert(a);
Why is a === 1?
That the answer was trying to say was that b is equal to
function b() {
function a() {}
a = 10;
return;
}
Similar to
function b() {
var a = function () {};
a = 10;
return;
}
i.e. there is an identifier a defined in b, so
function b() {
var a = 10;
return;
}
And now, obviously, the global a will not be modified by b
Please note that the position of the var isn't really important, just that it's there, the following will produce the same behaviour
function b() {
a = 10;
return;
var a;
}
The difference is that the first line is defined at run-time, whereas second line is defined at parse-time for a script block.
Look at the full answer on stack, here : var functionName = function() {} vs function functionName() {}
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.