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.