2012-08-14 3 views
0

가능한 중복 :
why self defining function reference keeps pointing to the old function자바 스크립트를 참조 혼란

내가 자바 스크립트 참조 동작과 매우 혼란 스러워요. 새로운 메모리 위치를 생성하지 않는 참조를 전달하는 데 매우 명확한 javscript 인이 코드를 살펴보십시오.

Human.prototype = Monkey.prototype; 
Human.prototype.name = "human"; 
Human.prototype.work = "love"; 

Joker.prototype = Human.prototype; 
Joker.prototype.name = "joker"; 
Joker.prototype.act = "awesome"; 

joker = new Joker(); 
human = new Human(); 
human.name = 'joker'; 

는 지금은 거의 명확 자바 스크립트

var scareMe = function() { 
    alert("Boo!"); 
    scareMe = function() { 
    alert("Double boo!"); 
    }; 
}; 

var prank = scareMe; 
prank(); // "Boo!" 
prank(); // "Boo!" 

scareMe(); // "Double boo!" 

날이 동작을 이해하는 데 도움을 주시기 바랍니다이 별도의 메모리 scareMe에 대한 위치와 장난을 만드는 것입니다 이것 봐.

+1

무엇이 당신의 질문입니까? 첫 번째 또는 두 번째 코드 블록에 대해 묻고 있습니까? 어떤 행동을 이해하지 못합니까? – JJJ

+2

이전에 http://stackoverflow.com/questions/11898651/why-self-defining-function-reference-keeps-pointing-to-the-old-function/11899017#11899017 질문 했습니까? 명확하지 않은 점은 무엇입니까? – xdazz

+0

누군가를 참고로 삼아 다른 이유와 다른 이유를 설명하십시오. –

답변

0

첫 번째 블록 :

Monkey = function(){}; 

Human = function(){}; 
Human.prototype = new Monkey(); // This is how you have to inherit properties of Monkey 
Human.prototype.name = "human"; 
Human.prototype.work = "love"; 

Joker= function(){}; 
Joker.prototype = new Human(); // This is how you have to inherit properties of Human 
Joker.prototype.name = "joker"; 
Joker.prototype.act = "awesome"; 

joker = new Joker(); 
human = new Human(); 
alert(joker.name); // shows joker 
alert(human.name); // shows human 

사실은 프로토 타입 속성을 생성하지 않는 것이 좋습니다. (참조 : http://css.dzone.com/articles/ecmascriptnext-classes)

Monkey = function(){ 
    this.name = "monkey"; 
    this.work = "jump"; 
}; 

Human = function(){ 
    this.name = "human"; 
    this.work = "love"; 
}; 
Human.prototype = new Monkey(); // This is how you have to inherit properties of Monkey 

Joker = function(){ 
    this.name = "joker"; 
    this.work = "awesome"; 
}; 

Joker.prototype = new Human(); // This is how you have to inherit properties of Human 

joker = new Joker(); 
human = new Human(); 
alert(joker.name); // shows joker 
alert(human.name); // shows human 

두 번째 블록 : 첫 번째 예에서

var scareMe = function() {  // `scareMe` is a global variable 
    alert("Boo!"); 
    scareMe = function() {  // value of global `scareMe` will be changed when original `scareMe` gets executed for the first time 
    alert("Double boo!"); 
    }; 
}; 

var prank = scareMe; 
prank(); // "Boo!" - because `prank` is original scareMe. but this execution changes value of `scareMe` 
prank(); // "Boo!" - same happens 

scareMe(); // "Double boo!" - `scareMe` is having new function. it shows "Double boo!" 
0

, 난 당신이 할당로 프로토 타입의 과제가 장착되지 않은 개체 상속, 하고있었습니다 생각 기존 프로토 타입에 프로토 타입을 추가하면 왼쪽 프로토 타입이 다른 프로토 타입을 참조하게됩니다.

Human.prototype = Monkey.prototype; 
//Human.prototype.name = "human"; -- not here 
Human.prototype.work = "love"; 

Joker.prototype = Human.prototype; 
Joker.prototype.name = "joker"; 
Joker.prototype.act = "awesome"; 

Human.prototype.name = "human"; // but here 

joker = new Joker(); 
human = new Human(); 
alert(human.name) // outputs "human" 

그러나이 대신에 객체 상속 사용한다 : 두 번째 예에서

Human.prototype = new Monkey(); 

를, 장난 는 그런 다음 코드에서 당신이 그 프로토 타입 변수, 예를 할당 할 경우의 문제입니다 원래 scareMe 함수가 할당됩니다. scareMe 함수는 "Boo!"를 출력합니다. scareMe functionvariable을 재 할당합니다. 이것은 가장 바깥 쪽의 "var scareMe"가 다른 함수를 가리킨다는 것을 의미합니다 ("Double Boo!"를 반환합니다). 하지만 장난이 여전히 원래 함수를 가리 키므로 원래 코드를 항상 실행합니다 (출력 "Boo!"및 scareMe 재 지정).

// i.e.: reverse the call order: 
scareMe(); // "Boo!" 
prank(); // "Boo!" 
prank(); // "Boo!" 

"scareMe"의 첫 번째 호출 scareMe를 재 할당하지만 장난은 여전히 ​​원래의 함수를 가리 킵니다.