express - Can an object be undefined but yet have a defined property? (Javascript) -


i have embarrassing question relating simple javascript function have come across:

passport.prototype.use = function(name, strategy) {   if (!strategy) {     strategy = name;     name = strategy.name;   }   if (!name) throw new error('authentication strategies must have name');    this._strategies[name] = strategy;   return this; }; 

i believe purpose of function give strategy name, overriding default name may have.

i believe first part of function assigning strategy.name name, given case strategy undefined if(!strategy){}. not feel intuitive me. how can strategy.name defined if code ran if strategy not defined? ie. can undefined object have defined property—or looking @ incorrectly?

as side note—i've been scouring web trying figure out use of _ throughout javascript. know underscore.js library pretty popular, library hasn't been loaded underscore must signify else.

anyways, appreciated. thanks!

for organization sake's, let's formalize comments section (btw, why many people answer via comments nowadays?)

argument overloading

this (unfortunately) quite common in javascript. it's way provide two interfaces same function. in opinion, leads confusion.

nonetheless, in case, author wants offer 2 signatures:

prototype.use(strategy: object)

and

prototype.use(name: string, strategy: string)

allowing caller either:

passport.use("name", "strategy");

or

passport.use({ "name": "name" });

therefore, if second argument falsy (if (!strategy)) use first argument instead (strategy = name;).

"""private""" variables

javascript lacks "private" variables (except closures) using underscore prefix _property indicates should not accessed external code, i.e. "use @ own peril, may break".


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -