开发者问题收集

两个对象,一个“不是函数”

2017-03-26
340

在代码底部,我构建了两个 Node 实例,它们都使用定义的 .getChildren 函数,但我得到了 Uncaught TypeError: t2.getChildren is not a function ,但我在 Node 对象构造函数中明确定义了它。另外,为什么它对 t2 而不是 t1 说了这句话。

function Node(value) {

    // based on graph theory, we give
    this.value = value;
    this.children = [];
    this.parent = null;

    // set and get functions
    this.setParent = function(node) {
        this.parent = node;
    };

    this.getParent = function() {
        return this.parent;
    };

    this.addChild = function(node) {
        node.setParent(this);
          this.children[this.children.length] = node;
    };

    this.getChildren = function() {
        return this.children;
    };

}


// check Identical

var subTreeFinder = function (t1,t2) {

  // base cases 

  if (t2==null) {
    return true; // empty trees are subtrees to all trees
  }

  if (t1==null){
    return false; // a non-empty tree can't fit in an empty tree
  }

  if (checkIdentical(t1,t2)) {
    return true;
  }

  var t1Children = t1.getChildren();
  var t2Children = t2.getChildren(); 

  return subTreeFinder(t1Children.every(subTreeFinder) || t2Children.every(subTreeFinder)); 
}


// check Identical
    var checkIdentical = function (t1,t2) {

      // base case
      if (t1==null && t2 == null) {
        return true;
      }


      if (t1 != null && t2 != null) {

        var t1Children = t1.getChildren();
        var t2Children = t2.getChildren();

        // an obvious case and time saver
        if (t1Children.length != t2Children.length){ 
          return false;
        }

        // recursive call. for every arr. element in both sets of kids check if we recurrsively get back true
        if (t1.value == t2.value && t1Children.every(checkIdentical) && t2Children.every(checkIdentical) ) {
          return true;
        }

      }

      // here: either one is null and the other ins't, so false.
      return false; 

    }

// The actual Trees

var dom = new Node('a'); // root
dom.addChild(new Node('b'));
dom.addChild(new Node('c'));

var vdom = new Node('x'); // root
vdom.addChild(new Node('y'));
vdom.addChild(new Node('z'));

console.log(subTreeFinder(dom, vdom));

// console.log(dom); // Should read entire Demo Tree object (unfold to see contents)
2个回答

您的代码对 Array.prototype.every 的实现有误。 请在此处检查正确的语法: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every

请参阅下文:更正后的代码

function Node(value) {

    // based on graph theory, we give
    this.value = value;
    this.children = [];
    this.parent = null;

    // set and get functions
    this.setParent = function(node) {
        this.parent = node;
    };

    this.getParent = function() {
        return this.parent;
    };

    this.addChild = function(node) {
        node.setParent(this);
          this.children[this.children.length] = node;
    };

    this.getChildren = function() {
        return this.children;
    };

}


// check Identical

var subTreeFinder = function (t1,t2) {
console.error(t1, t2)
  // base cases 

  if (t2==null) {
    return true; // empty trees are subtrees to all trees
  }

  if (t1==null){
    return false; // a non-empty tree can't fit in an empty tree
  }

  if (checkIdentical(t1,t2)) {
    return true;
  }
debugger;
  var t1Children = t1.getChildren();
  var t2Children = t2.getChildren(); 

  return subTreeFinder(t1Children.every(function(element) {subTreeFinder(element)}) || t2Children.every(function(element) {subTreeFinder(element)})); 
}


// check Identical
    var checkIdentical = function (t1,t2) {

      // base case
      if (t1==null && t2 == null) {
        return true;
      }


      if (t1 != null && t2 != null) {

        var t1Children = t1.getChildren();
        var t2Children = t2.getChildren();

        // an obvious case and time saver
        if (t1Children.length != t2Children.length){ 
          return false;
        }

        // recursive call. for every arr. element in both sets of kids check if we recurrsively get back true
        if (t1.value == t2.value && t1Children.every(checkIdentical) && t2Children.every(checkIdentical) ) {
          return true;
        }

      }

      // here: either one is null and the other ins't, so false.
      return false; 

    }

// The actual Trees

var dom = new Node('a'); // root
dom.addChild(new Node('b'));
dom.addChild(new Node('c'));

var vdom = new Node('x'); // root
vdom.addChild(new Node('y'));
vdom.addChild(new Node('z'));

console.log(subTreeFinder(dom, vdom));
Ayush Walia
2017-03-26

您错过了 Array#every 的 API,其中回调的第一个参数是实际项目,第二个参数是实际项目的索引。

索引不是 Node 的实例。因此方法 getChildren 不可用。

Nina Scholz
2017-03-26