call a function from another scope - javascript

please help to run a function that is in a different scope
have the following code:
function a(){
var rrr = 8;
function aim(arg){
console.log('aim' + arg);
console.log(rrr);
}
};
function b(){
a.aim('this is argument');
};
call a.aim ('this is argument'); does not work, the console displays a message
Uncaught ReferenceError: a is not defined
tried to call through apply. also unsuccessfully

using revealing module pattern:
var a = function(){
var rrr = 8;
function aim(arg){
console.log('aim' + arg);
console.log(rrr);
}
return {
aim: aim
}
}();
function b() {
a.aim('test');
}

function a(){
var rrr = 8;
return function aim(arg){
console.log('aim' + arg);
console.log(rrr);
}
};
function b(){
var aim = a();
aim('this is argument');
};

If you want to refer to a function as an object you need to create it first. Also, aim should be a property of this function (class)
function a() {
var rrr = 8;
this.aim = function(arg) {
console.log('aim' + arg);
console.log(rrr);
}
};
function b() {
var aa = new a();
aa.aim('this is argument');
}

You need the 2 minor changes:
function a(){
var rrr = 8;
this.aim = function(arg){
console.log('aim' + arg);
console.log(rrr);
}
};
var aa = new a();
function b(){
aa.aim('this is argument');
};

Related

Pass a function as a parameter in JS and access the receiving function's variables

I'm not sure this is possible in Javascript. I want to access a function's variable through a passed function, (or if a function is passed via an 'onend' call).
function Outer() {
var fn;
var foo = 'this is foo';
this.bar = function(x) {
fn();
}
this.setFunction = function(f) {
fn = f;
}
}
var o = new Outer();
o.setFunction(function() {
alert(foo); //doesn't work
});
o.bar(); //want to alert 'this is foo'
fn in your case is functionally a callback. Call the callback with the local foo variable as an argument:
function Outer() {
var fn;
var foo = 'this is foo';
this.bar = function() {
fn(foo);
}
this.setFunction = function(f) {
fn = f;
}
}
var o = new Outer();
o.setFunction(function(foo) {
alert(foo);
});
o.bar();
Could you try this
function Outer() {
var fn;
this.foo = 'this is foo';
this.bar = function(x) {
fn();
}
this.setFunction = function(f) {
fn = f;
}
}
var o = new Outer();
o.setFunction(function() {
alert(o.foo);
});
o.bar(); //want to alert 'this is foo'
and tell us if is what you need.
thanks

How to access variables declared in a function, from another function using javascript

In Javascript i need to access variables declared in a function from another function like:
function abc()
{
var a = 'StackOverflow';
}
Outside that function abc i need to access variable a
I tried like:
var s = function abc()
{
var a = 'StackOverflow';
}
alert(s.a);
I can access value of a by declaring it as global variable but i want to know how to access it from reference of the function abc
Please solve this issue
Thank You.
Try this:
function abc()
{
this.a = 'StackOverflow';
this.b = 'jQuery Core';
this.c = 'JavaScript';
}
var s = new abc();
alert( s.a );
Alternatively, if you have no other operations use this notation:
var s = {
a: 'StackOverflow',
b: 'jQuery Core',
c: 'JavaScript'
};
alert( s.a );
That's called JavaScript objects! Use this way:
var s = new function abc()
{
this.a = 'StackOverflow';
}
And call this way:
s.a; , e.g alert(s.a);
Or in other way:
var s = function abc()
{
var a = 'StackOverflow';
return {
a: a
};
}
And you can get it like:
s().a;
You can take a function with own property:
var s = function () {
function abc(r) { // declare function with one parameter as example
return Math.PI * r * r; // do something, here return area of circle as example
}
abc.a = 'StackOverflow'; // set property a with value
return abc; // return function with property
}(); // IIFE
document.getElementById('out').innerHTML = s.a + '<br>' + s(3);
<div id="out"></div>

Javascript: Run a function defined outside this closure as if it were defined inside this closure

I want to have a function A that accepts another function B as an argument, and then runs B as it were defined within the closure scope of A, i.e. has access to all the local variables.
For example, simplistically:
var A = function(B){
var localC = "hi";
B();
}
var B = function(){
console.log(localC);
}
A(B); // to log 'hi'
The only way I have found is to use eval. Does ec6 give any better options maybe?
One solution is to pass localC as argument in function B:
var A = function(B) {
var localC = "hi";
B(localC);
}
var B = function(localC) {
console.log(localC);
}
A(B); // outputs hi
Alternative using arguments:
var A = function(B) {
var localC = "hi";
B(localC, "test");
}
var B = function() {
var i = 0;
for (i; i < arguments.length; i++) {
console.log(arguments[i]);
}
}
A(B); // outputs hi, test
You can make the context explicit and pass it to B:
var A = function(B){
var context = {
localC: "hi"
};
B(context);
}
var B = function(context){
console.log(context.localC);
}
A(B); // hi
You can also use this with new and prototype:
var A = function() {
this.localC = "hi";
}
A.prototype.b = function(context){
console.log(this.localC);
}
var a = new A();
a.b(); // hi
or without the prototype:
var A = function() {
this.localC = "hi";
}
var a = new A();
a.b = function(context){
console.log(this.localC);
};
a.b(); // hi
You can use this with bind:
var a = {
localC: "hi"
};
function B(foo) {
console.log(this.localC, foo);
}
B.bind(a)("foo"); // hi foo
// .call:
B.call(a, "foo"); // hi foo
bind sets the context for this. call takes the context as it's first argument.
This one is not good:
var A = function(B){
var localC = "hi";
B.bind(this)(); // this is the global object, you need `new` to create a new scope
}
var B = function(){
console.log(this.localC);
}
A(B); // undefined
var A = function(B){
var self = this;
self.localC = "hi";
self.localD = "hello";
B();
};
var B = function(){
var self=this;
alert(self.localD);
}
A(B); // to log 'hi'

JavaScript scope with calling functions of the same class

It works fine as the following:
function A() {
}
A.prototype.f1 = function() {
alert('f1');
};
A.prototype.f2 = function() {
// calls f1
A.prototype.f1();
};
var a = new A();
a.f2(); // alert f1 correctly
But there's a function B to make A undefined to window scope, but can be accessed inside B scope:
function A() {
}
A.prototype.f1 = function() {
alert('f1');
};
A.prototype.f2 = function() {
// calls f1
A.prototype.f1();
};
function B() {
var PrivateA = null;
this.makePrivate = function() {
PrivateA = A; // private access
A = undefined; // undefined with window object
};
this.callA = function() {
var a = new PrivateA();
a.f2(); // it calls A.prototype.f1();, but A is undefined now
};
}
var b = new B();
// expect to accessible
var a = new A();
b.makePrivate();
// expect to inaccessible to window
alert(typeof A); // expect to be 'undefined'
b.callA(); // expect to alert 'f1', which not works now since A is undefined
I want make A accessible before B is called and A inaccessible when B is called.
Please give some advice.
You can set up B to look like this:
function B() {
var PrivateA; // It's not accessible from outside of B's scope
this.makePrivate = function() {
PrivateA = A; // Still in B's scope, so it works
A = undefined;
};
this.callA = function() {
var a = new PrivateA(); // So is this
a.f2();
};
}
Here's what happens when I run it:
> var b = new B();
> A
function A() {
this.f1 = function() {
alert('f1');
};
this.f2 = function() {
// calls f1
this.f1();
};
}
> b.makePrivate();
> A
undefined
> b.callA(); // I get an alert that says 'f1'

Javascript: Member-variable-accessing closures

I'm wondering how to deal with member variables in closures in JavaScript. The following code alerts "6".
function testy()
{
function blah()
{
this.a = 5;
this.func = function()
{
this.a = 6;
alert(this.a);
}
}
var x = new blah;
x.func();
}
but this code alerts 5.
function testy()
{
function execute(func)
{
func();
}
function blah()
{
this.a = 5;
this.func = function()
{
execute(function()
{
this.a = 6;
});
alert(this.a);
}
}
var x = new blah;
x.func();
}
How do I pass a closure which still accesses the member variables of the enclosing object?
execute(function()
{
this.a = 6;
});
function execute(func)
{
func();
}
Your calling the function as func(); and by default without specifying a context this will resolve to the global context which is window in the browser.. There are three options you can use here.
make this local
var that = this;
execute(function()
{
that.a = 6;
});
Now that points to the correct this.
bind this scope to the function
execute((function()
{
this.a = 6;
}).bind(this));
This will bind the correct / expected this scope to your function. Note that Function.prototype.bind is ES5 and will break older browsers. _.bind is a reasonable cross browser alternative.
edit execute
function execute(f, context) {
f.call(context);
}
execute(function() {
this.a = 6;
}, this);
Your passing the context as an extra parameter to execute. Then execute will call Function.prototype.call to make sure that the function is called with the desired context
Try this:
function blah()
{
this.a = 5;
this.func = function()
{
var self = this;
execute(function()
{
self.a = 6;
});
alert(this.a);
}
}

Categories