Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/372.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_Object - Fatal编程技术网

Javascript 两个对象,一个“;不是一个函数;

Javascript 两个对象,一个“;不是一个函数;,javascript,object,Javascript,Object,在代码的底部,我构建了两个节点实例,它们都使用定义的.getChildren函数,但我得到了uncaughtTypeError:t2.getChildren不是一个函数,但我在我的节点对象构造函数中明确定义了它。还有,为什么它说的是t2而不是t1 function Node(value) { // based on graph theory, we give this.value = value; this.children = []; this.parent

在代码的底部,我构建了两个节点实例,它们都使用定义的.getChildren函数,但我得到了
uncaughtTypeError:t2.getChildren不是一个函数,但我在我的节点对象构造函数中明确定义了它。还有,为什么它说的是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)
您缺少的API,其中回调的第一个参数是实际项,第二个参数是实际项的索引


索引不是
节点的实例。因此,方法
getChildren
不可用。

您的代码对Array.prototype.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));

现在还不清楚
subTreeFinder
checkidential
在做什么。