tags:

views:

121

answers:

7

I found this in a file called common.js:

function openBrowserWindow(theURL, winName, features) {
    window.open(theURL, winName, features);
}

I am trying to figure out what the intention of the person who wrote it was (or whatever program generated it.) It seems like it just mirrors the DOM window.open. What was the purpose? Why would you simply wrap a global method?

The function winds up resting in the window object. I did a console.log(window) and saw openBrowserWindow is there. What is strange is that window.open is not listed.

+2  A: 

It looks like it's just a more friendly named wrapper for the window.open function.

Chris
Do you think its friendlier? If a new guy has to fix a bug in your code, first time he sees `openBrowserWindow` he has to find out that its the same than `window.open` (which is even shorter)
voyager
@voyager - It’s not necessarily a name that I would have chosen, but on face value it is just a wrapper for window.open.
Chris
A: 

Why it is there is impossible to know, the fact that you may not want it there is more the issue.

You should look at all the places where it is being used and see if there may be some logic to how it is used, otherwise just get rid of the function and inline this function call.

I would wrap a function if I was currying it, but I doubt that is the case.

Here is an article on currying: http://ejohn.org/blog/partial-functions-in-javascript/

James Black
ahh, so you would return this (or maybe window?) to allow for chaining. not that it would be particularly useful in this scenario.
Shawn Simon
If I just wanted to change the features then doing this may make sense, as everything else is set once. That is the only reason I can see for adding an extra function.
James Black
+10  A: 

It's a encapsulation function. If you have to update how the window opens later (add more options, change the size etc.), it's easier to update the one function than update all the places which call window.open.

Kevin
Without seeing the context, I would have to agree with you.
Molex
@Molex you're right without seeing the rest of the code, I could be totally off, but that is my best guess considering the information at hand.
Kevin
Without the encapsulation, you would not necessarily have to update all the places that call window.open. I've been in that situation before. I was not stuck with updating everything. I simply put this at the top of the common JS file: `var winOpen = window.open; window.open = function(a, b, c) { return winOpen(a, b, c + "resize=1,status=1"); }`
Josh Stodola
@Josh: that works, but it's an ugly hack. With the encapsulating function, someone not aware of your changes can at least still call the original window.open() and not be terribly surprised...
Wim
@Wim I both agree and disagree. Josh's technique allows developers to forego familiarity with random frameworks and reduces coupling. The downside is tracking down the source of standard methods behaving differently and flexibility if parameters ever need to be overridden in one-off cases.
Justin Johnson
A: 

I would like to hope that its some smart-ass trying to get listed on The Daily WTF, but I have a deep suspicion that its just what it looks like: a function to wrap the window.open() method.

Since you're asking I'm guessing that there's no documentation. Do you have some context for where its being used?

AnonJr
it doesnt actually get called anywhere. im cleaning up some very old code, and since theres no documentation it's hard to separate the wtfs from the actual code
Shawn Simon
@unknown: delete it and don't look back. (Keep backups)
voyager
+1  A: 

The only advantage I can think of wrapping existing functions in your own functions is to leave room for future extensibility. You could then add extra parameters in future without breaking existing code. It's a basic form of encapsulation.

Dan Diplo
good point. on a side point did you know that you can send any number of parameters to any javascript function? theyll be in the this.arguments array : )
Shawn Simon
A: 

It is an encapsulation function.

Probably it's there in the case the developer finds any browser specific hacks that needs to be done, instead of applying the changes in every place where he needs to create a new window, he could put the hack in the encapsulation function instead.

Seth Illgard
+1  A: 

I'm thinking the developer probably forgot to finish the function so that it would check to see if the new window was being blocked by a popup blocker.

Here, let me finish it for you:

function openBrowserWindow(theURL, winName, features) {
    var MyWin = window.open(theURL, winName, features);
    if (MyWin==null) {
        alert("It appears that a pop-up blocker is preventing me from opening the new  window.\r\n\r\nTurn off your pop-up blocker or try Ctrl-Click next time.");
    }
}

You're welcome. ;)

BoltBait