programing

javascript/jQuery에 php의 isset 같은 것이 있습니까?

coolbiz 2023. 1. 27. 21:59
반응형

javascript/jQuery에 php의 isset 같은 것이 있습니까?

javascript/jQuery에 변수 설정/사용 가능 여부를 확인할 수 있는 내용이 있습니까?php에서는isset($variable)이런 걸 확인하려고요.

감사해요.

다음 식을 사용해 보십시오.

typeof(variable) != "undefined" && variable !== null

이것은 변수가 정의되어 있고 null이 아닌 경우에 해당됩니다.이것은 PHP의 isset의 동작 방식과 동일합니다.

다음과 같이 사용할 수 있습니다.

if(typeof(variable) != "undefined" && variable !== null) {
    bla();
}

PHP JS의 JavaScript isset()

function isset () {
    // discuss at: http://phpjs.org/functions/isset
    // +   original by: Kevin van     Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: FremyCompany
    // +   improved by: Onno Marsman
    // +   improved by: Rafał Kukawski
    // *     example 1: isset( undefined, true);
    // *     returns 1: false
    // *     example 2: isset( 'Kevin van Zonneveld' );
    // *     returns 2: true
    var a = arguments,
        l = a.length,
        i = 0,
        undef;

    if (l === 0) {
        throw new Error('Empty isset');
    }

    while (i !== l) {
        if (a[i] === undef || a[i] === null) {
            return false;
        }
        i++;
    }
    return true;
}

타입 오브는 제 생각에

if(typeof foo != "undefined"){}

속성이 존재하는지 확인하는 경우: hasOwnProperty를 사용하는 것이 좋습니다.

그리고 대부분의 객체는 다른 객체의 속성이기 때문에 (결국에는)windowobject) 이것은 값이 선언되었는지 확인하기 위해 잘 작동합니다.

각 답변의 일부는 유효합니다.질문대로 함수 "isset"으로 컴파일하여 PHP에서와 같이 동작합니다.

// isset helper function 
var isset = function(variable){
    return typeof(variable) !== "undefined" && variable !== null && variable !== '';
}

다음으로 사용 방법의 예를 나타냅니다.

var example = 'this is an example';
if(isset(example)){
    console.log('the example variable has a value set');
}

필요한 상황에 따라 다르지만 각 파트의 기능에 대해 설명하겠습니다.

  1. typeof(variable) !== "undefined"변수가 정의되어 있는지 여부를 확인합니다.
  2. variable !== null변수가 null인지 확인합니다(명시적으로 null을 설정하고 null로 설정되어 있는 경우는 그것이 옳다고 생각하지 않습니다.이 경우는 이 부분을 삭제해 주세요).
  3. variable !== ''변수가 빈 문자열로 설정되어 있는지 여부를 확인합니다.빈 문자열이 사용 사례에 대해 설정된 것으로 간주되면 이 문자열을 삭제할 수 있습니다.

이것이 누군가에게 도움이 되기를 바랍니다:)

자연스럽지 않아, 아니야...그러나 구글에서 검색한 결과, 다음과 같이 나타났습니다.http://phpjs.org/functions/isset:454

http://phpjs.org/functions/isset:454

phpjs 프로젝트는 신뢰할 수 있는 소스입니다.많은 jsent php 함수를 사용할 수 있습니다.오래 전부터 사용하고 있습니다만, 지금까지의 문제는 없었습니다.

문제는 정의되지 않은 변수를 함수에 전달하면 오류가 발생한다는 것입니다.

즉, 인수로서 전달하기 전에 타입 오브를 실행해야 합니다.

가장 깔끔한 방법은 다음과 같습니다.

function isset(v){
    if(v === 'undefined'){
        return false;
    }
    return true;
}

사용방법:

if(isset(typeof(varname))){
  alert('is set');
} else {
  alert('not set');
}

이제 그 코드는 훨씬 더 작고 읽기 쉽다.

인스턴스화되지 않은 변수에서 다음과 같은 변수를 호출하려고 해도 오류가 발생합니다.

isset(typeof(undefVar.subkey))

따라서 이 작업을 수행하기 전에 개체가 정의되어 있는지 확인해야 합니다.

undefVar = isset(typeof(undefVar))?undefVar:{};

여기서 :)

function isSet(iVal){
 return (iVal!=="" && iVal!=null && iVal!==undefined && typeof(iVal) != "undefined") ? 1 : 0;
} // Returns 1 if set, 0 false

@param-vikström의 대답에 덧붙여,variable!=null 해당되다variable!==null 함께variable!==undefined (오류)typeof(variable)!="undefined"를 참조해 주세요.

다음 작업을 수행할 수 있습니다.

if(variable||variable===0){
    //Yes it is set
    //do something
}
else {
    //No it is not set
    //Or its null
    //do something else 
}

언급URL : https://stackoverflow.com/questions/4231789/is-there-something-like-isset-of-php-in-javascript-jquery

반응형