JavaScript-如何注入不同的功能实现,例如警报(与web&;Node.js服务器端共享模块)
情况:有大量的JavaScript文件。我想在Node.js上运行它们 但是有几个地方使用了导致Node.js失败的警报() 当然,有一种方法可以查看每个文件并添加类似导入的内容JavaScript-如何注入不同的功能实现,例如警报(与web&;Node.js服务器端共享模块),javascript,node.js,function,implementation,inject,Javascript,Node.js,Function,Implementation,Inject,情况:有大量的JavaScript文件。我想在Node.js上运行它们 但是有几个地方使用了导致Node.js失败的警报() 当然,有一种方法可以查看每个文件并添加类似导入的内容 alert=require('console')。日志 但这将阻止这些文件在浏览器(客户端)中工作 是否有方法注入不同的警报实现?即在不修改源的情况下更改/添加函数实现?在代码开头,写下: global.alert=console.log在代码的开头,写下: global.alert=console.log基本版本 在
alert=require('console')。日志
但这将阻止这些文件在浏览器(客户端)中工作
是否有方法注入不同的警报实现?
即在不修改源的情况下更改/添加函数实现?在代码开头,写下:
global.alert=console.log代码>在代码的开头,写下:
global.alert=console.log代码>基本版本
在文件中silentalert.js
if(typeof global != "undefined"){
global.alert = typeof console != "undefined" && console.log ? console.log.bind(this) : function(){};
}
var g = typeof global != "undefined" ? global : (typeof window != "undefined") ? window : {};
var c = typeof console != "undefined" && console.log ? console.log.bind(this) : function(){};
var _old = g.alert;
var silentalert = function(activate){
g.alert = activate ? c : _old;
};
if(typeof module !== "undefined"){module.exports = silentalert;}
在NodeJS脚本中:
require('./silentalert');
此代码将在NodeJS中将警报消息打印到console.log
,但在浏览器中运行时仍将使用alert
下一个实现提供了一种更通用的方法
跨平台版本
在文件中silentalert.js
if(typeof global != "undefined"){
global.alert = typeof console != "undefined" && console.log ? console.log.bind(this) : function(){};
}
var g = typeof global != "undefined" ? global : (typeof window != "undefined") ? window : {};
var c = typeof console != "undefined" && console.log ? console.log.bind(this) : function(){};
var _old = g.alert;
var silentalert = function(activate){
g.alert = activate ? c : _old;
};
if(typeof module !== "undefined"){module.exports = silentalert;}
在NodeJS脚本中:
var silentalert = require('./silentalert');
silentalert(true);
// or silentalert(typeof window == "undefined") if you just want to silent alert() on NodeJS
// your script...
silentalert(false);
您甚至可以在HTML页面中直接包含silentalert.js:
<script src="./silentalert.js" type="text/javascript"></script>
<script type="text/javascript">
silentalert(true);
// your script...
silentalert(false);
</script>
与
这两个脚本都允许您在NodeJ中静音警报,同时仍然能够在客户端使用它们。Basic version
在文件中silentalert.js
if(typeof global != "undefined"){
global.alert = typeof console != "undefined" && console.log ? console.log.bind(this) : function(){};
}
var g = typeof global != "undefined" ? global : (typeof window != "undefined") ? window : {};
var c = typeof console != "undefined" && console.log ? console.log.bind(this) : function(){};
var _old = g.alert;
var silentalert = function(activate){
g.alert = activate ? c : _old;
};
if(typeof module !== "undefined"){module.exports = silentalert;}
在NodeJS脚本中:
require('./silentalert');
此代码将在NodeJS中将警报消息打印到console.log
,但在浏览器中运行时仍将使用alert
下一个实现提供了一种更通用的方法
跨平台版本
在文件中silentalert.js
if(typeof global != "undefined"){
global.alert = typeof console != "undefined" && console.log ? console.log.bind(this) : function(){};
}
var g = typeof global != "undefined" ? global : (typeof window != "undefined") ? window : {};
var c = typeof console != "undefined" && console.log ? console.log.bind(this) : function(){};
var _old = g.alert;
var silentalert = function(activate){
g.alert = activate ? c : _old;
};
if(typeof module !== "undefined"){module.exports = silentalert;}
在NodeJS脚本中:
var silentalert = require('./silentalert');
silentalert(true);
// or silentalert(typeof window == "undefined") if you just want to silent alert() on NodeJS
// your script...
silentalert(false);
您甚至可以在HTML页面中直接包含silentalert.js:
<script src="./silentalert.js" type="text/javascript"></script>
<script type="text/javascript">
silentalert(true);
// your script...
silentalert(false);
</script>
与
这两个脚本都允许您在NodeJ中静默警报,同时仍然能够在客户端使用它们