Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
452 views
in Technique[技术] by (71.8m points)

html - can i listen to a function call using javascript?

How can i listen to a specific function call with parametters using javascript:

example : when showname(2) is called i can do something like call another function too, showage(2)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

you can wrap it:

var native = window.alert;

window.alert = function(){
    console.log('alerting...');
    native.apply(window, arguments);
    console.log('alerted!');
};

alert('test');

update

you can do something similar with properties, using getters and/or setters:

var foo = {
     bar = 'baz'
};

into

var foo = {
    _bar: 'baz',
    get bar(){
        console.log('someone is taking my bar!');
        return this._bar;
    },
    set bar(val){
        console.log('someone pretends to set my bar to "' + val + '"!');
        this._bar = val;
    }
};

alert(foo.bar);

foo.bar = 'taz';

encapsulated (private _bar):

var foo = function(){
    var _bar = 'baz';

    return {
        get bar(){
            console.log('someone is taking my bar!');
            return _bar;
        },
        set bar(val){
            console.log('someone pretends to set my bar to "' + val + '"!');
            _bar = val;
        }
    };
}();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...