2012-12-04 6 views
0

왜 작동하지 않는, 그것은 기능이 아니라고, 왜 myalert2에서 myalert()를 호출 할 수 없습니까 ?? 수행하는 방법??내부 함수를 호출하는 방법?

var sheepclass = function(handler) { 
    this.handler = $.extend({ 
     'sizes': 'thin', 
     'eat': 'grass', 
     'color': 'white', 
     myalert: function() { 
      alert('Hello World'); 
     }, 
     myalert2: function() { 
      handler.myalert(); 
      this.handler.myalert(); //this not work either 
     } 
    }, handler); 
} 
var blacksheep = new sheepclass({ 
    'color': 'black' 
}); 
blacksheep.handler.myalert2(); 

답변

0

이 때문에 범위의의를 할 것입니다 this.myalert() 전화 ...

http://jsfiddle.net/78QXc/

var sheepclass = function(handler) { 
    this.handler = $.extend({ 
     'sizes': 'thin', 
     'eat': 'grass', 
     'color': 'white', 
     myalert: function() { 
      alert('Hello World'); 
     }, 
     myalert2: function() { 
      this.myalert(); 
     } 
    }, handler); 
} 
var blacksheep = new sheepclass({ 
    'color': 'black' 
}); 
blacksheep.handler.myalert2();​ 
0

그냥

+0

예 와우, 왜? 그것은 handler.something이어야한다. 나는 모든 것을 내부적으로 handler.size, hander.xxx()로 만들었다. 그런 다음 이것들을 모두 이상하게 변경해야한다. ... 이상한 ... – FatDogMark

+0

핸들러 객체를 확장하면 'this'는 실제로 핸들러 객체 자체를 참조합니다. 그래서 this.handler를 쓸 필요가 없다. – Stanley

관련 문제