gpt4 book ai didi

javascript - 两个对象,一个 "Is not a function"

转载 作者:太空宇宙 更新时间:2023-11-04 15:52:01 24 4
gpt4 key购买 nike

在代码的底部,我构建了两个节点实例,它们都使用定义的 .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)

最佳答案

您的代码错误地实现了 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));

关于javascript - 两个对象,一个 "Is not a function",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43030006/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com