Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/437.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript-不适用于动态对象的类型_Javascript - Fatal编程技术网

Javascript-不适用于动态对象的类型

Javascript-不适用于动态对象的类型,javascript,Javascript,未创建以下任何子对象时 if(typeof this.obj[child1][child2]!=="undefined"){ } 但是,上述方法不起作用 if(typeof this.obj[child1]!=="undefined"){ } 这是因为typeof的优先级低于[]或的优先级,因此首先执行[],并抛出错误: > typeof foo == "undefined" true > typeof foo.bar == "undefined" ReferenceError:

未创建以下任何子对象时

if(typeof this.obj[child1][child2]!=="undefined"){
}
但是,上述方法不起作用

if(typeof this.obj[child1]!=="undefined"){
}

这是因为
typeof
的优先级低于
[]
的优先级,因此首先执行
[]
,并抛出错误:

> typeof foo == "undefined"
true
> typeof foo.bar == "undefined"
ReferenceError: foo is not defined
要检查嵌套属性的长链,可以使用如下函数:

function hasKeys(obj, keys) {
    for(var i = 0; i < keys.length; i++) {
        if(typeof obj[keys[i]] == "undefined")
            return false;
        obj = obj[keys[i]];
    }
    return true;
}

if(hasKeys(this, ["obj", "child1", "child2"])) ...
var test = {child1: {child2: "TEST"}};
alert(is_defined(test, 'child1', 'child2')); // true
alert(is_defined(test, 'child1', 'child2', 'child3')); // false

function is_defined(obj) {
    if (typeof obj === 'undefined') return false;

    for (var i = 1, len = arguments.length; i < len; ++i) {
        if (obj[arguments[i]]) {
            obj = obj[arguments[i]];
        } else if (i == (len - 1) && typeof obj[arguments[i] !== 'undefined']) {
            return true;
        } else {
            return false;
        }
    }

    return true;
}

使用undefined而不是“undefined”,或者尝试null。

在第一种情况下使用此选项-

if(this.obj[child1] && typeof this.obj[child1][child2]!=="undefined"){
}

这不起作用,因为它将抛出JavaScript错误。它将抛出这个。obj[child1]未定义。你应该经常检查它是否也被定义了

if(this.obj[child1] && typeof this.obj[child1][child2]!=="undefined"){
}
编辑

如果要提高代码可读性,可以使用如下函数:

function hasKeys(obj, keys) {
    for(var i = 0; i < keys.length; i++) {
        if(typeof obj[keys[i]] == "undefined")
            return false;
        obj = obj[keys[i]];
    }
    return true;
}

if(hasKeys(this, ["obj", "child1", "child2"])) ...
var test = {child1: {child2: "TEST"}};
alert(is_defined(test, 'child1', 'child2')); // true
alert(is_defined(test, 'child1', 'child2', 'child3')); // false

function is_defined(obj) {
    if (typeof obj === 'undefined') return false;

    for (var i = 1, len = arguments.length; i < len; ++i) {
        if (obj[arguments[i]]) {
            obj = obj[arguments[i]];
        } else if (i == (len - 1) && typeof obj[arguments[i] !== 'undefined']) {
            return true;
        } else {
            return false;
        }
    }

    return true;
}
var test={child1:{child2:“test”};
警报(已定义(测试“child1”、“child2”);//真的
警报(已定义(测试“child1”、“child2”、“child3”);//假的
功能已定义(obj){
if(typeof obj==='undefined')返回false;
for(变量i=1,len=arguments.length;i
您实际上不需要……“的
类型未定义“
以确定
此.obj[child1][child2]
是否未定义。而是使用:

if (this.obj && this.obj[child1] && this.obj[child1][child2]) {}
例如:

var obj = {};
obj.c1 = {};
alert( obj && obj.c1 && obj.c1.c2 ? 'obj.c1.c2 exists' : 'nope'); 
     //=> "nope"
obj.c1.c2 = 1;
alert( obj && obj.c1 && obj.c1.c2 ? 'obj.c1.c2 exists' : 'nope'); 
     //=> "obj.c1.c2 exists"
您可以创建一个函数来确定任何
对象中是否存在某个路径

function pathExists(root,path){
  var pathx = path.constructor === Array && path || path.split(/\./)
    , trial
    , pathok = true
  ;

  while (pathx.length && pathok) {
    trial = pathx.shift();
    pathok = (root = root && root[trial] || false, root);
  }
  return pathok;
}
// usage examples
var obj = {c1:1};
pathExists(obj,'c1.c2'); //=> false
pathExists(obj,'c1'); //=> 1
obj.c1 = { c2: {c3: 3} };
pathExists(obj,['c1','c2','c3']); //=> 3
// your case
if ( pathExists(this,[obj,child1,child2]) ) { /*...*/ }

if(typeof this.obj[child1]!==“undefined”&&typeof this.obj[child1][child2]!==“undefined”)
如何?如果在这两种情况下
this.obj[child1]
都是
未定义的
那么
这个.obj[child1][child2]
将抛出一个类似
无法访问属性的错误。。。对于未定义的
可能他还应该检查
this.obj[child1]
是否是一个对象。@RienNeVaPlus在这种情况下,
this.obj[child1]
会失败吗?不,这不是必需的。如果
this.obj[child1]
不是一个对象,并且是一个文本,那么javascript会在检查
child2
项之前自动将其转换为一个对象。@RienNeVaPlus必须是
未定义的
null
未定义的
不是保留关键字,因此测试
某物的类型===“undefined”
是一种省钱的方法。而
undefined
null
不是一回事。好的,实际上还有更多的孩子,所以我希望避免这样做,但我想这是不可能的。如果
this.obj[child1]
is
null
类型错误:无法读取null的属性“…”
将被抛出。实际上这对我不起作用,因为第一个参数导致条件传递为true,我只希望在最后一个参数为true时条件传递为true。@t.niese完全正确!请参阅我解决该问题的答案。相同仅当最后一个子项存在时,发出条件必须通过。但我可以在if/else语句中放入try吗?