访问函数错误是什么意思?

4

正在运行 Node.js@0.8.15 + Express@3.0.4 + Jade@0.27.7 + Stylus@0.31.0。 由于某种原因,出现了以下错误。有人知道这是什么意思吗?

我不认为我做了什么奇怪的事情。当我执行 res.render(view, response); 时发生这种情况。

Property 'visitFunction' of object #<Object> is not a function
    at Object.Compiler.visitNode (/app/node_modules/jade/lib/compiler.js:176:32)
    at Object.Compiler.visit (/app/node_modules/jade/lib/compiler.js:161:10)
    at Object.Compiler.visitBlock (/app/node_modules/jade/lib/compiler.js:253:12)
    at Object.Compiler.visitNode (/app/node_modules/jade/lib/compiler.js:176:32)
    at Object.Compiler.visit (/app/node_modules/jade/lib/compiler.js:161:10)
    at Object.Compiler.compile (/app/node_modules/jade/lib/compiler.js:78:10)
    at parse (/app/node_modules/jade/lib/jade.js:101:23)
    at Object.exports.compile (/app/node_modules/jade/lib/jade.js:163:9)
    at Object.exports.render (/app/node_modules/jade/lib/jade.js:215:17)
    at View.exports.renderFile [as engine] (/app/node_modules/jade/lib/jade.js:243:13)

这个版本的Jade肯定有bug了吧?我把我的Jade版本降级到0.25,现在它能正常工作了。通过以下命令进行降级:npm install jade@0.25 - Jon Derring
1个回答

6
您可能会遇到这个错误的原因之一是因为您向Object.prototype添加了新属性(通常是方法)。
例如:
Object.prototype.someNewMethod = function (value1, value2) {
    // ... perform some operations
    return this;
};

那种向Object添加新属性的方式不推荐,正如#1033中在express项目中所述。应该使用Object.defineProperty,并将enumerable设置为false

使用Object.defineProperty扩展Object的示例。

Object.defineProperty(
    Object.prototype, 
    'someNewMethod',
    {
        writable : false,
        // Will not show up in enumerable properties (including for-in loop).
        enumerable : false, 
        configurable : false,
        value : function (value1, value2) {
            // ... perform some operations
            return this;
        }
    }
);

我曾经遇到过完全相同的问题,使用Object.definePropertyenumerable:false来定义新属性解决了这个问题。

希望这能有所帮助。


谢谢您提供简单而又出色的答案。但是为什么会发生这种情况呢? - alioguzhan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接