이 가이드]1를 따라 새로운 JS-플래시 통신을 만들었습니다.
내 코드는 다음과 같습니다.
function getID( swfID ){
if(navigator.appName.indexOf("Microsoft") != -1){
me = window[swfID];
}else{
me = document[swfID];
}
}
function js_to_as( str ){
me.onChange(str);
}
그러나 때때로 내 온체인지
가 로드되지 않습니다. 다음과 같은 파이어버그 오류
me.onChange는 함수가 아닙니다.
내 프로그램에서 가장 중요한 기능이 아니기 때문에 우아하게 저하시키고 싶습니다. typeof`도 같은 오류가 발생합니다.
그것이 존재하는지 확인한 다음 온 체인지
만 실행하는 방법에 대한 제안 사항이 있습니까?
(아래 방법 중 어느 것도 하나의 작업을 잡으려고 시도하지 않습니다).
다음과 같은 방법을 시도해 보세요:
if (typeof me.onChange !== "undefined") {
// safe to use the function
}
또는 그보다 더 나은 방법(업더크리크의 업보트 코멘트에 따라)
if (typeof me.onChange === "function") {
// safe to use the function
}
내가 가진 이 문제가 발생합니다.
if (obj && typeof obj === 'function') { ... }
계속 될 경우 오류 발생 참조입니다 obj 상태입니까 정의되지 않았습니다.
결국 내가 정말 다음과 같다.
if (typeof obj !== 'undefined' && typeof obj === 'function') { ... }
간단해진다는:
if (typeof obj === 'function') { ... }
훨씬 더 및 작동됨 멋지구리해요.
var functionString = "nonexsitantFunction"
eval("typeof " + functionString) // returns "undefined" or "function"
var functionString = "nonexsitantFunction"
typeof(eval(functionString)) // returns ReferenceError: [function] is not defined
어때.
if('functionName' in Obj){
//code
}
예:
var color1 = new String("green");
"length" in color1 // returns true
"indexOf" in color1 // returns true
"blablabla" in color1 // returns false
과 단축시킵니다 사례:
if('onChange' in me){
//code
}
참조 매든 docs.
메이온천게 & &; 메이온천게 (str).
기본적으로 메이온천게 경우 정의되지 않았습니다. (이 경우 그동안 이니시에이트한 hasn& 될 것이라고 # 39, t), 그런 won& t # 39 는 기능, 그렇지 않을 경우 하반기에 실행하십시오 메이온천게 autoexec. 메이온천게 (str).
더 나아가 검색하기를 할 수도 있습니다.
me && me.onChange && me.onChange(str);
이런 me 는 비동기 잘 알려져 있다.
function thisishere() {
return false;
}
alert("thisishere() is a " + typeof thisishere);
alert("thisisnthere() is " + typeof thisisnthere);
또는 표시됨과 경우:
if (typeof thisishere === 'function') {
// function exists
}
또는 수신기마다 반환 값, 한 줄에:
var exists = (typeof thisishere === 'function') ? "Value if true" : "Value if false";
var exists = (typeof thisishere === 'function') // Returns true or false
//Simple function that will tell if the function is defined or not
function is_function(func) {
return typeof window[func] !== 'undefined' && $.isFunction(window[func]);
}
//usage
if (is_function("myFunction") {
alert("myFunction defined");
} else {
alert("myFunction not defined");
}
이 방법으로 이 언더스코엘가이스 라이브러리란 eventtest 이펑크션 (설명을 할 수 있는 제안하세요 중시하는 일부 브라우저 버그) 이
typeof obj == 'function' || false
http://underscorejs.org/docs/underscore.html # 섹선에서 143
이 방법을 사용하면 좋다.
function isFunction(functionToCheck) {
var getType = {};
return functionToCheck && getType.toString.call(functionToCheck) === '[object Function]';
}
사용법:
if ( isFunction(me.onChange) ) {
me.onChange(str); // call the function with params
}
나도 날잡아 이 수락됨 대답. 그러나:
console.log(typeof me.onChange);
다음 날 위해 오토메이티드 변경됩니까 오리지널 수락됨 일했다.
if (typeof me.onchange === "function") {
// safe to use the function
}
function function_exists(function_name)
{
return eval('typeof ' + function_name) === 'function';
}
alert(function_exists('test'));
alert(function_exists('function_exists'));
또는
function function_exists(func_name) {
// discuss at: http://phpjs.org/functions/function_exists/
// original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// improved by: Steve Clay
// improved by: Legaev Andrey
// improved by: Brett Zamir (http://brett-zamir.me)
// example 1: function_exists('isFinite');
// returns 1: true
if (typeof func_name === 'string') {
func_name = this.window[func_name];
}
return typeof func_name === 'function';
}