在$ .extend类中如何调用内部函数?

时间:2012-12-04 09:18:10

标签: javascript jquery

为什么它不起作用,它说它不是一个函数,为什么我不能从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();

2 个答案:

答案 0 :(得分:0)

只需致电this.myalert()即可

答案 1 :(得分:0)

这是因为范围...

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();​
相关问题