I found two different ways to define / name objects and functions in JavaScript that first check for a name before using it. The problem is that I don’t know which one is better (in terms of speed and usability), and it’s impossible to use logical operators in a Google search to figure this out.
The first one I see most often:
var myNewObject = myNewObject ? myNewObject : function () {
The second one looks shorter, but I saw only one or two places, so I don’t know if there is a standard there or even a name for it:
var myNewObject = myNewObject || function() {
Functionally, they both do the same thing, and both of them seem to work in every browser in which I can test. My question is: which is better and why? Furthermore, although the first definition is essentially a one-line conditional ... what is called the second?
javascript object
Eamann
source share