Shawn
Shawn

Reputation: 19793

What is the point of wrapping built-in browser API in a function?

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.

Upvotes: 0

Views: 275

Answers (7)

BoltBait
BoltBait

Reputation: 11489

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. ;)

Upvotes: 1

Daniel Rodriguez
Daniel Rodriguez

Reputation: 1483

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.

Upvotes: 0

Dan Diplo
Dan Diplo

Reputation: 25339

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.

Upvotes: 1

AnonJr
AnonJr

Reputation: 2757

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?

Upvotes: 0

kemiller2002
kemiller2002

Reputation: 115420

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.

Upvotes: 10

James Black
James Black

Reputation: 41858

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/

Upvotes: 0

Chris
Chris

Reputation: 6315

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

Upvotes: 2

Related Questions