This question already has answers here:
JavaScript inner-functions and Performance
(2 answers)
Closed 8 years ago.
If i have code:
function A() {
function B() {
}
B();
}
A();
A();
is B function parsed and created each time i call A(so it can decrease performance of A)?
If you want use a function only internally, how about closure. Here an example
var A = (function () {
var publicFun = function () { console.log("I'm public"); }
var privateFun2 = function () { console.log("I'm private"); }
console.log("call from the inside");
publicFun();
privateFun2();
return {
publicFun: publicFun
}
})();
console.log("call from the outside");
A.publicFun();
A.privateFun(); //error, because this function unavailable
function A(){
function B(){
}
var F=function(){
B();
}
return F;
}
var X=A();
//Now when u want to use this just use this X function it will work without parsing 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()
This question already has answers here:
Javascript function scoping and hoisting
(18 answers)
Closed 2 years ago.
In the example below, I can call the functional expression b from the functional expression a even though b() is not defined at that point. Could you explain why this works?
Thanks in advance!
function go() {
const a = () => {
b();
}
const b = () => {
console.log("abc")
}
a();
}
go();
It has been defined by the time the a function runs. a() is below const b =, so b has been defined.
But it wouldn't work if b was defined after
function go() {
const a = () => {
b();
}
a();
const b = () => {
console.log("abc")
}
}
go();
It's OK for a function body to reference something that hasn't been defined yet. But it's not OK for that function to run before the variable has been defined.
This question already has answers here:
dynamically call local function in javascript
(5 answers)
Closed 8 years ago.
I was wondering if it's possible to call a function by passing a string name. Following is the basic architecture:
Javascript:
"use strict";
function foo(){
var f = this;
f.fn = function(o){return fn(o)}
function fn(o){
o.name();
}
function a(){
alert('a');
}
function b(){
alert('bb');
}
}
var f = new foo();
f.fn({name:'a'});
f.fn({name:'b'});
The code is setup at http://jsfiddle.net/rexonms/9c7bnkc9/.
You can achieve it using eval:
function foo(){
var f = this;
f.fn = function(o){return fn(o)}
function fn(o){
eval(o.name + '()');
}
function a(){
alert('a');
}
function b(){
alert('bb');
}
}
var f = new foo();
f.fn({name:'a'});
f.fn({name:'b'});
Here is jsFiddle: http://jsfiddle.net/9c7bnkc9/2/
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();
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>