calling a function from outside of a function in javascript - javascript

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();

Related

How to call a specific function from inside a subfunction in 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()

How to call a function within another function in javascript

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()();

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();

How to call a parent method from within a child method?

I have such a code:
function A() {
this.hello = function() {
console.log("I'm A");
}
}
function B() {
this.hello = function() {
// I need to call A.hello here, like parent.hello();
B.prototype.hello(); // This is wrong, TypeError
console.log("I'm B");
}
}
B.prototype = new A();
var b = new B();
b.hello();
#=> TypeError: Cannot call method 'hello' of undefined
I read some similar questions here but they all use this technique, they assign a method to a prototype.
FaqPage.prototype.init = function(name, faq) {
BasePage.prototype.init.call(this, name);
this.faq = faq;
}
FaqPage.prototype.getFaq = function() {
return this.faq;
}
But it is not in my case. My prototype is a parent's instance. How may call a parent method in my case? Or do I have to refactor my code?
You need to assign the this.hello a value, at the moment you are just creating a function to run.
Try the following :
function A() {
this.hello = function() {
console.log("I'm A");
}
}
function B() {
this.hello = function() {
B.prototype.hello(); // Now runs correctly and logs "I'm A"
console.log("I'm B");
}
}
B.prototype = new A();
var b = new B();
b.hello();
By changing the code to be this.hello = function() { } we are creating a property of the object that can be called from outside the object.
The result of calling b.hello(); is :
I'm A
I'm B
Example JSFiddle

Categories