console.log不是nodejs 6.9.1中的函数 [英] console.log is not a function in nodejs 6.9.1
问题描述
当我尝试理解Javascript中的代码时,我遇到了一个奇怪的问题:好的部分。我尝试使用console.log()打印一些东西,但我得到TypeError,我的代码在这里:
I met a weird problem when I try to understand the code in the Javascript:The Good Parts. I try to use console.log() to print something, but I just get TypeError, my code is here:
Function.prototype.method=function(name,func){
this.prototype[name]=func;
return this;
}
Function.method('bind',function(that){
var method=this;
var slice =Array.prototype.slice;
var args=slice.apply(arguments,[1]);
console.log(that);//error: console.log is not a function
return function(){
return method.apply(that,args.concat(slice.apply(arguments,[0])));
};
});
var x=function(){
return this.value;
}.bind({value:666});
console.log(x());
并且错误消息是:
/home/wz/code/js/c.js:14
console.log(that);//error: console.log is not a function
^
TypeError: console.log is not a function
at Function.<anonymous> (/home/wz/code/js/c.js:14:13)
at new Console (console.js:34:23)
at console.js:100:18
at NativeModule.compile (bootstrap_node.js:497:7)
at Function.NativeModule.require (bootstrap_node.js:438:18)
at get (bootstrap_node.js:254:34)
at Function.<anonymous> (/home/wz/code/js/c.js:14:5)
at Object.<anonymous> (/home/wz/code/js/c.js:23:3)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
Shell 已返回1
它是有趣的是,当我在stackoverflow中运行代码段时,它按预期工作...所以似乎我的原生环境中有一些错误?
It is so interesting when I run the snippet in stackoverflow it works as I expected... So it seems that there are some bugs in my native environment?
我尝试使用fs将console.log保存到文件中以查看我的代码中实际存在的内容,我将代码更改为:
I try to use fs to save the console.log into file to see what actually it is in my code, I change the code to:
fs=require('fs');
Function.prototype.method=function(name,func){
this.prototype[name]=func;
return this;
}
Function.method('bind',function(that){
var method=this;
var slice =Array.prototype.slice;
var args=slice.apply(arguments,[1]);
fs.writeFile('a.txt',String(console.log));
//console.log(that);//error: console.log is not a function
return function(){
return method.apply(that,args.concat(slice.apply(arguments,[0])));
};
});
var x=function(){
return this.value;
}.bind({value:666});
console.log(x());
和a.txt:
function (){
return method.apply(that,args.concat(slice.apply(arguments,[0])));
}
令人惊讶的是,console.log成为返回的对象...我完全是困惑。
我尝试了节点6.9.1和4.6.1,得到了相同的结果。我使用nvm来管理我的节点版本
So amazing that console.log becomes the returned object... I was totally confused. I have tried node 6.9.1 and 4.6.1 and I got the same result. And I use nvm to manage my node version
推荐答案
你覆盖 console
你代码中的某个地方。为避免 Monkey修补,您可以使用构造:
You override console
somewhere in your code. For avoid Monkey patching you can use construction:
console.log = console.log || function(message){alert(message);};
使用这种结构,你不会覆盖现有的功能,只添加新的方法或礼节。
With this construction you will not override existing functionality, only add new method or proprieties.
这篇关于console.log不是nodejs 6.9.1中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!