Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
76
rated 0 times [  82] [ 6]  / answers: 1 / hits: 17506  / 12 Years ago, thu, february 7, 2013, 12:00:00

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)


More From » html

 Answers
64

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;
}
};
}();

[#80364] Wednesday, February 6, 2013, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jeremiahianx

Total Points: 629
Total Questions: 106
Total Answers: 112

Location: Djibouti
Member since Sun, Feb 27, 2022
2 Years ago
;