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()();
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()
This question already has answers here:
Calling a Function defined inside another function in Javascript
(11 answers)
Closed 2 years ago.
function a(){
this.i = 10,
function b(){
console.log("Regular function ",this.i, this)
}
}
Here I want to call function b
Because of the use of this within the function, you need to call the a function as a "constructor function" (with the new keyword) or invoke it with call(), apply(), or bind() so that you can specify context for this.
And, because the b function uses the context of a, it should be either returned from the parent or declared as an instance method of a with this.b = function()...
function a() {
this.i = 10;
// To make the nested function have access to the
// parent function's instance data, you can either
// return it from the parent or declare it as an
// instance method of the parent (shown here):
this.b = function(){
console.log("Regular function ", this.i, this);
}
}
// Invoke a as a constructor function
let newA = new a();
// Now call b within the context of a
newA.b();
You need to return the function first as shown below:
function a(){
this.i = 10;
return function b(){
console.log("Regular function ",this.i, this)
}
}
And then call it using:
a()()
UPDATE: this keyword in the above example will point to the global scope. To make sure we use this within the function context we can use call() method on function a() and pass the object to which this will associate it's scope.
function a(){
this.i = 10;
return b = ()=>{
console.log("Regular function ",this.i,this)
}
}
a.call({i:10})();
Note: I have also used arrow method for function b() to keep this scope used within it as its function a()'s scope.
I am wondering how to access a function in camera.
I have no clue to access function a() and b() from outside.
When I run this code, I have undefined error in console.
var camera = (function () {
function a(){
console.log('calling ..a');
}
function b(){
console.log('calling ..b');
}
})();
//calling function a() from outside
camera.a();
You can return an object, which wraps both the functions:
var camera = (function() {
function a() {
console.log('calling ..a');
}
function b() {
console.log('calling ..b');
}
//exposing a and b as a public API
return {a, b}
})();
//calling function a() from outside
camera.a();
camera.b();
Now you created a private scope for external so you need to create a public scope.
var camera = (function () {
function a(){
console.log('calling ..a');
}
function b(){
console.log('calling ..b');
}
return {
a,
b
}
})();
//calling function a() from outside
camera.a();
The key of that is return the public references of the function.
return {
a,
b
}
function camera(){
this.a=function (){
console.log('calling ..a');
};
this.b=function (){
console.log('calling ..b');
};
}
//calling function a() from outside
var c=new camera();
c.b();
In this example, the greet() function is invoked through sample.greet().
let sample = {
greet() {
console.log("hi")
}
}
How to invoke the inner greet() function if defined like this?
function sample() {
function greet() {
console.log("hi")
}
}
Functions declared inside a function body such as the greet() function in your example here:
function sample() {
function greet() {
console.log("hi")
}
}
are private to within the function body and cannot be called from outside of the sample() function scope. You can only call greet() from within the sample() function body unless you somehow assign or return greet after running sample() so you purposely assign greet to some outside variable (which is not present in your example).
Functions are objects so you can create properties on those objects and can then assign a function to a property and can then call it:
function sample() {
console.log("running sample()");
}
sample.greet = function () {
console.log("hi")
}
sample.greet();
You can call the nested function by calling it within the enclosing function. This is because the scope of the nested function is limited to the enclosing function and there is no other way to call it from outside.
function sample() {
function greet() {
console.log("hi");
}
greet();
}
sample()
You can assign properties to functions the same way you can for objects. You can assign another function as one of those properties, which makes it like a method.
function foo(){
console.log("foo called");
}
foo.bar = function() {
console.log("foo.bar called");
}
foo(); // "foo called"
foo.bar(); // "foo.bar called"
You can also assign any value to a property on a function.
foo.x = 1;
console.log(foo.x); // 1
So yes, functions are objects and it is possible to treat them like objects.
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.