반환 키워드 그것을에 관하여 내가 객체를 생성하고 반환 내 자신의 <code>new</code> 기능을 만들었습니다
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()
문제는 그 난이 같은 반환 설정하는 경우 :
return constructor.apply(obj, myArray.slice(1)) || obj
작동을하지만, 할 때 나는 다음과 같은 반환 설정 :
return obj || constructor.apply(obj, myArray.slice(1))
그것이 말하는을 차이가 무엇을 할 수있다
정의되지 않은 이 둘을 트위터로?
그래서 constructor.apply (obj, myArray.slice (1))가 false 인 방법은 무엇입니까? –
@AhMedRaXa'constructor.apply (obj, myArray.slice (1)) '호출은 Douglas와 Crockford가있는 Person 함수를 인수로 호출합니다. 이 때문에 getName()을 호출하면 함수의 this.firstName과 this.lastName이 있습니다. –
'constructor.apply (obj, myArray.slice (1))'의 평가가 false가되어'||'의 오른쪽을 평가하는 것을 의미합니까? –