передача console.log в качестве параметра функции выдает Uncaught TypeError: недопустимый вызов

При тестировании JS-примера генерации двоичного дерева и кода обхода я заменил print() на console.log(), чтобы создать консольный вывод. «Неперехваченный... Недопустимый вызов» выдается в «case «this»: func (this.value);». Почему эту функцию нельзя передать как параметр функции, как и любую другую? Как видно из закомментированного кода, передача объекта «дерево» в console.log просто выводит код функции, а не .value. Может ли кто-нибудь дать рекомендации по обстоятельствам этого исключения или лучшему способу регистрации выходных данных теста?

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script type="text/javascript">
        //20140122-this example code originally had "print" statements in place of console.log, however, console.log cannot be passed as a function parameter.

        function BinaryTree(value, left, right) {
            this.value = value;
            this.left = left;
            this.right = right;
        }

        BinaryTree.prototype.preorder = function(f) { this.walk(f, ['this', 'left', 'right']); };
        BinaryTree.prototype.inorder = function(f) { this.walk(f, ['left', 'this', 'right']); };
        BinaryTree.prototype.postorder = function(f) { this.walk(f, ['left', 'right', 'this']); };
        BinaryTree.prototype.walk = function(func, order) {
            for (var i in order)
                switch (order[i]) {
                case "this":
                    func(this.value);
                    break;
                case "left":
                    if (this.left) this.left.walk(func, order);
                    break;
                case "right":
                    if (this.right) this.right.walk(func, order);
                    break;
                }
        };
        BinaryTree.prototype.levelorder = function(func) {
            var queue = [this];
            while (queue.length != 0) {
                var node = queue.shift();
                func(node.value);
                if (node.left) queue.push(node.left);
                if (node.right) queue.push(node.right);
            }
        };

        // convenience function for creating a binary tree
        function createBinaryTreeFromArray(ary) {
            var left = null, right = null;
            if (ary[1]) left = createBinaryTreeFromArray(ary[1]);
            if (ary[2]) right = createBinaryTreeFromArray(ary[2]);
            return new BinaryTree(ary[0], left, right);
        }

        var tree = createBinaryTreeFromArray([1, [2, [4, [7]], [5]], [3, [6, [8], [9]]]]);

        console.log("*** preorder ***"); tree.preorder(console.log);
        console.log("*** inorder ***"); tree.inorder(console.log);
        console.log("*** postorder ***"); tree.postorder(console.log);
        console.log("*** levelorder ***"); tree.levelorder(console.log);
//        console.log("*** preorder ***"); console.log(tree.preorder);
//        console.log("*** inorder ***"); console.log(tree.inorder);
//        console.log("*** postorder ***"); console.log(tree.postorder);
//        console.log("*** levelorder ***"); console.log(tree.levelorder);
//    </script>
</head>
<body>
</body>
</html>

person robWDC    schedule 23.01.2014    source источник


Ответы (1)


Когда вы передаете метод объекта в качестве параметра функции, вы теряете контекст. Чтобы сохранить его, просто привяжите метод к контексту:

tree.levelorder(console.log.bind(console));

Ex:

[1,2,3].forEach(console.log)
> TypeError: Illegal invocation

[1,2,3].forEach(console.log.bind(console))
> 1 0 [1, 2, 3]
> 2 1 [1, 2, 3]
> 3 2 [1, 2, 3] 
person Tibos    schedule 23.01.2014