gpt4 book ai didi

调用函数事件的 JavaScript/jQuery

转载 作者:行者123 更新时间:2023-11-29 14:44:09 25 4
gpt4 key购买 nike

在 JavaScript/jQuery 中是否有内置的调用函数事件?

我的意思是 on('call' , function(){})

我同时使用这个自定义事件:

function setCookie(name, value, days) {
// set ...
$(document).trigger( "setCookieEvent", [ name ] );
}
function cookieController()
{
$(document).on( "setCookieEvent", function( event, name ) {
//setCookie name
});
}
cookieController();

最佳答案

如上面的评论所述,您可以使用一些新功能来修饰原始函数。您实质上是在保存对原始函数的引用,并重写原始函数以包含一些新功能。

function setCookie( name, value, days ) {
console.log('setCookieCalled ' + name, value, days );
}

setCookie('[before decoration]', 'foo', 1 );

// rewrite the setCookie function
setCookie = decorate(setCookie, function( name, value, days ){
console.log('decorated setCookie ' + name, value, days );
});

// this will modify the original function. and return a new function that decorates the old.
function decorate(originalFunction, decorator) {
return function( /* arguments */ ) {
console.log('called decorator ' + arguments[0] );
// call the decorator and the original function
decorator.apply( this, arguments );
originalFunction.apply( this, arguments );
}
}

setCookie('[after decoration]', 'bar', 2 );
<script src="http://codepen.io/synthet1c/pen/WrQapG.js"></script>

关于调用函数事件的 JavaScript/jQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34700702/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com