在嵌套对象中使用hasOwnproperty()

时间:2016-03-07 14:30:28

标签: javascript

我有以下数据:

console.log(trace.hasOwnProperty(trace.dataref.ydata));

我想检查对象是否具有属性ydata但是如果我运行以下命令,则返回'false'。你能用正确的语法帮助我吗?

public final class RandomComparator<T> implements Comparator<T> {

    private final Map<T, Integer> map = new IdentityHashMap<>();
    private final Random random;

    public RandomComparator() {
        this(new Random());
    }

    public RandomComparator(Random random) {
        this.random = random;
    }

    @Override
    public int compare(T t1, T t2) {
        return Integer.compare(valueFor(t1), valueFor(t2));
    }

    private int valueFor(T t) {
        synchronized (map) {
            return map.computeIfAbsent(t, ignore -> random.nextInt());
        }
    }

}

https://jsbin.com/jamohurota/edit?html,js,console

1 个答案:

答案 0 :(得分:3)

如果你在trace上调用hasOwnProperty,那么这就是要检查的对象。您需要在您怀疑拥有该属性的对象上调用它:

    trace.dataref.hasOwnProperty("ydata");