0

I was used to do programming using python but I am using javascript for some project now. In python, OOP concept was little easy than javascript which I am reading right now. That being said, I am not so comfortable right now using javascript object and prototypes. In the meanwhile, can I achieve the following without using object? I am not a pure programmer (always took it as hobby) so please bear with me:

function func1(arg1){

does item..1..for eg...var result=arg1*2;
does item..2..for eg...var resultSq=arg*arg;
......
}

I want other function, say func2 which when calls func1; funct1 responds just by performing item...1 and returning the result.

In other words, I want to detect who called the function and have if statements to change the execution Sorry if it is too simple.

Jack_of_All_Trades
  • 10,942
  • 18
  • 58
  • 88

2 Answers2

1

Identifying who the Caller is:

<script>

function func1()
{
    if(arguments.callee.caller.toString().indexOf(" test()") != -1) 
    //May be optimizable with a name property
    {
        return "I was called from test()";
    }   

     if(arguments.callee.caller.toString().indexOf(" test2()") != -1)   
    //May be optimizable with a name property
    {
        return "I was called from test2()";
    }

}

function test()
{
    return func1();
}

function test2()
{
    return func1();
}

alert(test());
</script>

Reference: How do you find out the caller function in JavaScript?

Passing an ID:

As the question is actually how to execute differently depending on who the caller is, you need to have an if statement, that checks what action should be executed.

function doSomething(actionNumber)
{

if(actionNumber == 1)
{
return 1;
}
else if(actionNumber == 2)
{
return 1;
}

//etc...
}

function2()
{
doSomething(1);
}
Community
  • 1
  • 1
Menelaos
  • 23,508
  • 18
  • 90
  • 155
  • I do not intend to func2 to call another function which is internal to func1...I want func1 to behave differently for func2's call. In my case, just execute fewer lines of code of the same function and break, and return the value. – Jack_of_All_Trades Jun 21 '13 at 17:03
  • exactly! That is the goal. – Jack_of_All_Trades Jun 21 '13 at 17:05
  • Just keep in mind that `arguments.callee` is not allowed in JavaScript strict mode. – bfavaretto Jun 21 '13 at 17:23
  • [replicating-arguments-callee-in-strict-mode](http://stackoverflow.com/questions/10009682/replicating-arguments-callee-in-strict-mode) – Menelaos Jun 21 '13 at 17:29
  • 1
    @Jack_of_All_Trades The alternative is just not enabling strict mode (which is something you enable manually, by adding `"use strict";` to the top of a file or function). – bfavaretto Jun 21 '13 at 17:29
1

There are a number of patterns to create and handle objects, here is one that I use a lot.

JavaScript prototypal OOP

var ConstructFoo = function () { //capitalise constructor names
    var x = "my private variable";//remember, JS has FUNCTIONAL scope
    this.y = 10; //we can read and write to this outside of this function
}
ConstructFoo.prototype.barMethod = function () {
    return this.y * 10;
}
var bash = new ConstructFoo();//new keyword is syntactic sugar for creating a new instance
alert(bash.y);//will alert 10
alert(bash.x);//ERROR, this is undefined
alert(bash.barMethod());//will alert 100
var baz = new ConstructFoo(); //new instance, lets prove it
alert(bash === baz); //will alert false
bash = "";
alert(bash.y);//ERROR, this is undefined
alert(baz.y);//alerts 10

Not to be confused with a JavaScript object:

var foo = {
    funcA: function (params) {
       var bar = 2;
       //etc
       return bar;
    },
    funcB: function (param) {
        var bar = param * 2;
        //etc
        return bar;
    }
};

foo.funcA(7);//returns 2
foo.funcB(2); //returns 4

This is a static object. You don't create new instances of it. If you change foo.funcA elsewhere, it will affect all code using that function.

Suggested reading: JavaScript Patterns by O'Reilly

Dawn
  • 941
  • 6
  • 11