:
function foo(){
}
function bar(){
alert('something');
// not empty
}
console.log('foo is empty :' + isEmpty(foo));
console.log('bar is empty :' + isEmpty(bar));
function isEmpty(f) {
return typeof f === "function" && /^function [^(]*\(\)[ ]*{(.*)}$/.exec(
f.toString().replace(/\n/g, "")
)[1].trim() === "";
}
FIDDLE
을이 콜백을 확인하기 위해 단지 경우, 정상적인 방법으로는 확인된다
if (typeof callback === 'function') callback.call();
EDIT :
콜백 함수이면
는 의견을 무시하려면
function isEmpty(f) {
return typeof f === "function" && /^function [^(]*\(\)[ ]*{(.*)}$/.exec(
f.toString().replace(/\n/g, "").replace(/(\/\*[\w\'\s\r\n\*]*\*\/)|(\/\/[\w\s\']*)|(\<![\-\-\s\w\>\/]*\>)/g, '')
)[1].trim() === "";
}
FIDDLE
실제 사용은 무엇 일 것입니다 :
이 정규식을 수정, 여기에 변수에 대한 지원과 같은 기능은? – JJJ
특별히 JQuery를 사용하는 방법에 관심이 있으십니까? (JQuery! = JS) – nico
@Nico, 잘 아니, JS 물론 괜찮을 것이다. 방금 jQuery 함수가 이미 존재할 것이라고 생각했습니다. 이미 간과하고있었습니다. – Joey