0

My question is on the following expression:

var prototype = Object.create(extend && extend.prototype);

I think it's creating the prototype object inheriting the extend object. And extend && extend.prototype is checking to see if extend exists and extend.prototype exists. If either one doesn't exist, prototype will be a object without superclass. And if both exist, prototype will inherit extend? How does it know to use extend.prototype with extend && extend.prototype syntax?

So var prototype = Object.create(extend && extend.prototype) and var prototype = Object.create(extend.prototype) are the same if both extend and extend.prototype are there?

Thanks.

Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
techguy2000
  • 4,861
  • 6
  • 32
  • 48
  • I don't agree that the question is a duplicate, although that answer should help the OP understand this as well... – Scott Sauyet Mar 04 '15 at 15:54
  • @JDB: Only that this question is actually not about `Object.create` at all. – Felix Kling Mar 04 '15 at 15:56
  • @ScottSauyet Given that the hearts of the current two answers are "*It will return the last truthy value*" and "*The value of a `&&` operation is the value of the first "falsy" expression, or else the value of the last expression*", and given that the thesis of the accepted answer on my proposed duplicate is "*the Logical AND operator (`&&`) will return the value of the second operand if the first is truthy*" I don't see any substantial difference between the answers here. Granted, the OP asks for specific results based on certain conditions, but those are deducible from the general principle. – apsillers Mar 04 '15 at 15:58
  • @apsillers: But note the difficulties that this presents. I don't know where this code is from. But if `extend` is undefined, then this code will throw an error. The final question obviously can be answered with a simple "yes", but there's more here. – Scott Sauyet Mar 04 '15 at 16:05
  • This is from https://www.youtube.com/watch?v=ya4UHuXNygM 54:32 – techguy2000 Mar 05 '15 at 05:37

1 Answers1

2

The key is that in JavaScript, unlike other C-heritage languages, the && operator does not (necessarily) have a boolean result. The value of a && operation is the value of the first "falsy" expression, or else the value of the last expression. The tests it makes are performed on the coerced-to-boolean values of the subexpressions, but the resulting value is the raw value of whichever subexpression was the last to be evaluated.

This in the evaluation of

extend && extend.prototype

the resulting value will be either the value of the subexpression extend or of the subexpression extend.prototype. If extend is null or undefined or any other "falsy" value, then that value will be the overall result. Otherwise the value will be that of extend.prototype.

Note that if extend is, for example, a simple object, and specifically one without a property called prototype, the overall value may still be undefined. It's clear that the intent of the code in the question is to pick the prototype reference of a passed-in constructor function, but it's not "bulletproof" code.

Pointy
  • 405,095
  • 59
  • 585
  • 614