JavaScript Determine if dynamically named function exists

前端 未结 4 710
名媛妹妹
名媛妹妹 2021-01-01 08:15

How can I check if a dynamically named object or function exists?

In example:

var str = \'test\';
var obj_str = \'Page_\'+str;

function Pag         


        
4条回答
  •  没有蜡笔的小新
    2021-01-01 08:46

    if you run your code in browser, just access global object by window, your code may like this:

    if (typeof window[obj_str] === 'string') {
        alert('ok');
    }
    

    otherwise, you should gain access to global object:

    var global = function() { return this; }() || (1,eval)('this');
    if (typeof global[obj_str] === 'stribg
    

提交回复
热议问题