5

I have a javascript file in which I write a bunch of jquery functions. I have a function to return the angular scope. I found out that if I were to write the same function twice, the code still executes.

function getngScope()
{
    alert(2);
    return angular.element($('#data-area')).scope();
}

function getngScope()
{
    alert(1);
    return angular.element($('#data-area')).scope();
}

When I call getngScope() I get "1" alerted and the scope returned. Why does it have this behavior?

pgpb.padilla
  • 2,318
  • 2
  • 20
  • 44
Undefined Variable
  • 4,196
  • 10
  • 40
  • 69

4 Answers4

6

The second definition of an Object overwrites the first one. In general the last definition of an object overwrites all previous definitions.

Functions in JavaScript are Objects:

(function () {}) instanceof Object === true

When you create a new global function f it's equivalent to creating a property in the window object and assigning the function definition to that variable, if you create a function:

function myFun() { console.log('my function') };

and then check the value of window.myFun you'll notice it is the same function as myFun:

window.myFun === myFun // true

You'll also notice that modifying window.myFun changes/overwrites myFun.

E.g.

function myFun() { console.log('myFun') };
myFun(); // Prints: 'myFun'

// Overwrite myFun
window.myFun = function () { console.log('NoFun') };
myFun(); // Prints: 'NoFun'

The second definition of the function takes precedence.

I recommend you read the chapter on Functions from JavaScript: the good parts by Crockford.

Clint
  • 2,696
  • 23
  • 42
pgpb.padilla
  • 2,318
  • 2
  • 20
  • 44
1

functions are data in memory stack, so when you define another function with the same name, it overrides the previous one.

Zohaib Ijaz
  • 21,926
  • 7
  • 38
  • 60
0

Well obviously you’re not meant to define the same function twice. However, when you do, the latter definition is the only 1 that applies. Try not to do that. Find another way other than giving two functions the same name.

Y2H
  • 2,419
  • 1
  • 19
  • 37
  • I never realized that writing a new function will same name will overwrite the first one, in JS! – Undefined Variable Nov 28 '15 at 17:07
  • Not just in JS, pretty much ever language I’ve ever dealt with. Why are you trying to do that anyway? Maybe I can suggest another method for yo. – Y2H Nov 28 '15 at 17:08
  • @Y2H well Java and C will certainly complain about two functions with the same name in the same context. – Pointy Nov 28 '15 at 17:10
  • Haven’t used C in a while but Java won’t. – Y2H Nov 28 '15 at 17:11
  • @Pointy no they won’t. –  Nov 28 '15 at 17:11
  • You guys must be using some weird Java and C compilers. The Linux C compiler gives a "redefinition of foo" error if you redefine function foo in the same compilation unit, and Java will give an "... is already defined" error (assuming the signature is the same of course). – Pointy Nov 28 '15 at 17:29
  • @Y2H: I mainly work in PHP and in PHP it would. I also worked in C/C++ long time ago and I think it would complain in those languages as well. As for Java, wont it complain if the function has same name and signature/parameters? If I recollect correctly,using same name with different signatures is just Java's way of overloading methods, isnt it? – Undefined Variable Nov 28 '15 at 18:21
  • @Undefined Variable that is true. But in that case the methods would have have a different number of arguments. In your case both methods take 1 argument. Anyway, hope your problem is solved now. – Y2H Nov 28 '15 at 18:23
0

The second function replaced the first function,you could always change this by modifying the name of the function ,if not you can add multiple arguments ..if that is ever needed...and for the explaination to this behaviour,unlike other programming languages javascript doesnt return any errors while being executed..so u can assume that it just corrects itself during the execution by overwriting the function.

Shrikantha Budya
  • 646
  • 1
  • 4
  • 15