0

I have created my own new function.

function Person(firstName, lastName) {
this.firstName = firstName;  
this.lastName = lastName;
}

Person.prototype.getName = function() {  
console.log(this.firstName + " " + this.lastName)
}

function spawn(constructor) {
var obj = {}
Object.setPrototypeOf(obj, constructor.prototype)
var myArray = Array.prototype.slice.apply(arguments)
return constructor.apply(obj, myArray.slice(1)) || obj

}

var crockford = spawn(Person, "Douglas", "Crockford")
crockford.getName();

I am just confused about the operation of the logical operator ||.

constructor.apply(obj, myArray.slice(1)) || obj it seams that javascript engine first executes the left side of the code even it being falsy (means apply function is applied to constructor before returning obj) and then return the right side of the || operator. am i right? Consider the code below,

var app = constructor.apply(obj, myArray.slice(1)) return app || obj

in this case the constructor.apply(obj, myArray.slice(1)) is already applied to the constructor.it doesn't rely one the return value of logical operator.

I just want the clarification that does || operator executes both sides of the function before returning one or it just select one and the other one is not even executed.. i think it executes constructor.apply(obj, myArray.slice(1)) that why i am getting the result Douglas Crockford.

AhMed RaXa
  • 39
  • 7
  • I guess you could always just read the [*language specification*](http://ecma-international.org/ecma-262/7.0/index.html#sec-binary-logical-operators-runtime-semantics-evaluation). – RobG May 09 '17 at 05:06

3 Answers3

2

If you are checking for A operator B == true:

-In case of logical OR, the second operand is evaluated only if the first operand evaluates to false (0)

-In case of AND, the second operand is evaluated only if the first operand evaluates to true (1).

Aanchal1103
  • 917
  • 8
  • 21
  • so the first part will effect the code even being falsy? – AhMed RaXa May 09 '17 at 04:58
  • If you are checking for `A or B == true` and A is evaluated to true then your B won't be checked because The overall condition has already become true. – Aanchal1103 May 09 '17 at 05:00
  • But doesn't the || operator works like a selector(it has to choose one code of these two)? – AhMed RaXa May 09 '17 at 05:05
  • No, read what is written. If the first returns true then the second is not evaluated. If the first returns false, the second is unequivocally evaluated and its value returned, whether it's "truthy" or "falsey". – RobG May 09 '17 at 05:08
  • So the first part will also effect the code and even after being falsy it has the effect on the code and just not checked and left behind? – AhMed RaXa May 09 '17 at 05:44
0

The right hand side of either the '&&' and '||' logical operators will only be evaluated and returned if the left hand side is 'falsey' and 'truthy' respectively.

The left hand side will always be evaluated.

MDN Logical Operators

aaronw
  • 136
  • 2
  • 9
0

Both || and && operators checks

  1. only LHS if result is sufficient to guess the whole result ( i.e true in case of ||, and false in case of &&).

  2. checks both LHS and RHS if result of LHS is not sufficient to guess whole result. ( if LHS for || is false or LHS of && is true, then it checks for RHS also).

S Jayesh
  • 191
  • 1
  • 4
  • 19