Can a function be called using string value in strict mode [duplicate] - javascript

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/

Related

Function object and Function are they same? [duplicate]

This question already has answers here:
JavaScript difference between function and new function
(2 answers)
Closed 5 years ago.
Is
var myFunc = function() {
}
similar to myFunc = new function () { }
I read in the documentation it says both mean the same. What is the difference between these two?
They are not the same.
var myFunc = function(){} myFunc is a reference to anonymous function expression.
var myFunc = new function (){} reference to newly constructed instance of anonymous function expression.
var myFunc = function() {}
var myFunc1 = new function() {}
// it is the same as doing:
var myFunc2 = new myFunc;
console.log(myFunc, ' and the type is ', typeof myFunc)
console.log(myFunc1, ' and the type is ', typeof myFunc1)
console.log(myFunc2, ' and the type is ', typeof myFunc2)
You can reference this answer as well

Using variables from other functions in Javascript [duplicate]

This question already has answers here:
What is the scope of variables in JavaScript?
(27 answers)
Closed 6 years ago.
I have searched the internet trying to find a good answer for this question. What I mainly found is suggestions to move the variable in the global scope, or using the functions as parameters for the function I want to use the variable in, but without explanation on how that works
To explain my dilema, lets say we have this piece of code:
function foo(){
var x = 2;
}
function bar(){
var z = 2;
}
function compare(foo,bar){
if ( z === x ) {
console.log("text");
}
}
This is the problem I'm facing. Is the code I've written above correct and if I call the compare() function it should console log "text" ?
declare with globel variable it's will easy to pass Another function
var x;
var y;
function foo(){
x = 2;
}
function bar(){
z = 2;
}
function compare(){
if ( z === x ) {
console.log("text");
}
}
foo()
bar()
compare();

Function definition inside function [duplicate]

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

Call function automatically inside other functions [duplicate]

This question already has answers here:
Add a line of code to ALL functions
(3 answers)
Closed 8 years ago.
I currently have a function which I call at the end in several other functions. Instead of writing it like this:
function foo(){
baz();
}
function bar(){
baz();
}
function baz(){
}
Is there any way to make baz() run automatically when calling the funcions? Perhaps by modifying the prototype of the other functions and add it as an IIFE? Is this possible?
Do you mean something like "Template method" design pattern. If so, try this:
function inherit(proto) {
var F = function() { };
F.prototype = proto;
return new F();
}
var template = {
process: function() {
this.mainfunction();
this.baz();
}
baz:function(){
}
};
(function run() {
var foo = inherit(template);
foo.mainfunction = function(){
}
foo.process();
var bar = inherit(template);
bar.mainfunction = function(){
}
bar.process();
}())

What's the difference b/w old and new javascript function syntax? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
JavaScript: var functionName = function() {} vs function functionName() {}
How are Foo and Bar any different ?
If objects were only functions, why was this new syntax introduced ? (Foo).
var Foo = function(arg) {
this.attr = arg;
};
function Bar (arg) {
this.attr = arg;
}
/*
>>> f = new Foo(3)
Object { attr=3}
>>> f.attr
3
>>> b = new Bar(40)
Bar { attr=40}
>>> b.attr
40
*/
A fair amount of documentation I'v read proposes the first syntaxe, but the second one seems to work just as well.
The difference come here:
console.log(typeof foo); //'function'
function foo() {
}
console.log(typeof bar); //'undefined'
var bar = function () {
}

Categories