2012-12-17 2 views
0

그래서 프로토 타입에 관해 많은 책을 읽었으며 대부분 그것을 얻었습니다. 다음과 같은 것을 얻었습니다.함수와 객체를위한 프로토 타입 생성자

var Animal = function(species) { 
    this.species = species; 
}; 
Animal.prototype.getSpecies = function() { 
    return this.species; 
} 
var myDog = new Animal("Anderson");  
alert(myDog.getSpecies()); 

난) (나는 새로운 종을 만들어 동물에 프로토 타입을 설정 한 다음 getSpecies를 호출 할 수 있음을 이해합니다. 네!

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

var meToo = { fName: "ken", lName: "N" }; 
alert(meToo.constructor.prototype); // [object Object] 
alert(Person.constructor.prototype); // function Empty(){} 

http://jsfiddle.net/r0k3t/s8Sx7/9/

나는 사람에 대한 프로토 타입이 기능은 이유를 설명 무언가를 찾기 위해 노력했다() {} : 나를 혼란 무엇

이 무엇입니까? 전 세계적인 객체 인 'this'(이 경우는 윈도우)로 설정 될 것이라고 생각했습니다. 또한 왜 내가 그 속성을 열거 할 수 없습니까? 읽기 this 읽기 나는 constructor.prototype을 사용하여 'window'라고 생각한 객체를 검색 한 다음 속성을 열거 할 수 있다고 제안합니다.

분명히 나는 ​​뭔가를 놓치고 있습니다 - 감사합니다!

+0

시도의 CONSOLE.LOG (meToo.constructor.prototype)로 만든 것도 여기에 – mplungjan

+0

로깅 사용하여 업데이트 된 바이올린입니다 의미 http://jsfiddle.net/r0k3t/s8Sx7/ 14/ – Kenn

답변

3

Person 개체의 프로토 타입은 단지 Person.prototype입니다. 아니 다른 Person.constructor.prototype,

Person.constructor은 모든 기능을 구성하는 Function 함수입니다. Person은 함수이기 때문에 .constructorFunction입니다.

Function 개체 (모든 기능)의 프로토 타입은 단지 Function.prototype입니다. 그래서 Person.constructor.prototype === Function.prototype.

평범한 개체의 생성자는 Object 함수입니다. 모든 일반 개체의 프로토 타입은 Object.prototype이며 "[object Object]"입니다 (console.dir 이상 alert 이상).

일반 객체에 의해, 내가 {} 또는 new Object()

+0

그래서 Angus Croll이 "IE 이외의 모든 브라우저는 비표준 접근기 __proto__를 지원합니다."라는 오류가 발생하면 객체의 생성자에게 프로토 타입 속성을 요청할 수 없습니다. " Object.getPrototypeOf (myThing)와 같은 소리가 myThing.constructor.prototype과 동일한 것을 반환합니다. 제 말은, 당신이 말한 것을 이해하지만 분명히 뭔가를 놓치고 있습니다. 흠? – Kenn

+0

@Kenn 네, 생성자 속성이 간섭을받지 않는다면 : myThing .__ proto__ === Object.getPrototypeOf (myThing) === myThing.constructor.prototype' – Esailija

+0

@Kenn 그리고 새로운 Person 객체를 생성하면됩니다. 'person .__ proto__ === Object.getPrototypeOf (person) === person.constructor.prototype' 소문자'person.constructor'를 언급하고 있습니다. 'Person.constructor' – Esailija