我的 createMinHeightBST 函数中的 JavaScript 函数声明范围

问题描述 投票:0回答:2

遇到一些关于我的函数声明范围的奇怪 JavaScript 问题。在我的

BST
函数中,我可以毫无问题地调用函数
node
。但是在我的
createMinimalHeightBST
函数中,当我尝试调用
BST
函数时,出现错误。

错误提示:

TypeError: BST is not a function

请参阅注释行 WORKSDOESNT WORK


代码

var BST = new BST(10);

console.log(BST.root);


function node(data) {
    this.data = data;
    this.right = null;
    this.left = null;
}

function BST(root_data) {
    this.root = root_data === undefined ? root_data : new node(root_data); //WORKS
    this.addNode = function(node, root) {
        if (root === undefined) return this.addNode(node, this.root);
        if (!root) {
            root = node;
            return;
        }
        if (root.data > node.data) {
            if (!root.left) {
                root.left = node;
                return;
            }
            return this.addNode(node, root.left);
        }
        if (!root.right) {
            root.right = node;
            return;
        }  
        return this.addNode(node, root.right);
    };
}

var test = [2, 5, 9, 21, 50, 88];

function createMinimalHeightBST(sorted_arr) {
    var mid = Math.floor(sorted_arr.length / 2);
    var minimal_height_tree = new BST(sorted_arr[mid]); // DOESNT WORK

    for (var i=0; i<sorted_arr.length; i++) {
        if (i !== mid) minimal_height_tree.addNode(sorted_arr[i]);
    }

    return minimal_height_tree;
}

console.log(createMinimalHeightBST(test));
javascript function scope javascript-function-declaration
2个回答
1
投票

修复了代码:现在第一行中的函数与变量声明不再混淆 var BST = 新 BST_Function(10);

    console.log(BST.root);

    function node(data) {
        this.data = data;
        this.right = null;
        this.left = null;
    }

    function BST_Function(root_data) {
        this.root = root_data === undefined ? root_data : new node(root_data); //WORKS
        this.addNode = function (node, root) {
            if (root === undefined) return this.addNode(node, this.root);
            if (!root) {
                root = node;
                return;
            }
            if (root.data > node.data) {
                if (!root.left) {
                    root.left = node;
                    return;
                }
                return this.addNode(node, root.left);
            }
            if (!root.right) {
                root.right = node;
                return;
            }
            return this.addNode(node, root.right);
        };
    }

    var test = [2, 5, 9, 21, 50, 88];

    function createMinimalHeightBST(sorted_arr) {
        var mid = Math.floor(sorted_arr.length / 2);
        var minimal_height_tree = new BST_Function(sorted_arr[mid]); // DOESNT WORK

        for (var i = 0; i < sorted_arr.length; i++) {
            if (i !== mid) minimal_height_tree.addNode(sorted_arr[i]);
        }

        return minimal_height_tree;
    }

    console.log(createMinimalHeightBST(test));

0
投票

在构造函数之后定义

var BST = new BST(10);
,并将变量名称更改为不同的内容

© www.soinside.com 2019 - 2024. All rights reserved.