---
title: "酸爽的console.log"
author: "bee1314"
date: "2014-07-17T09:25:07Z"
tags: []
categories: ["Web前端"]
slug: "console-log"
draft: false
lastmod: "2014-07-17T09:45:42Z"
id: 2093014
url: "https://bee1314.iteye.com/blog/2093014"
---

在前端的开发中，console.log那是开发必备啊，简直直观。通过写小函数，组合大功能。更容易测试。但是在打版本时，就要删除console.log，打完版本进入开发状态又要添加，真不够爽。重复劳动太多。所以可以做些简单地封装，方便开发和上线。

```
/**
 * log.js hufeng
 * The safe wrapper for `console.xxx` functions
 *  log("message") ==> console.log("message")
 *  log("message", "warn") ==> console.warn("message")
 */

//cache current location hash, when the module loading
//only fetch hash one time.
var isDebug = parent.window.location.hash === '#debug';

module.exports = function() {	
  window.console &&
  // Do NOT print `log(msg)` in non-debug mode
  isDebug &&
  // Call native method of console
  // if not pass 'console' as first argument, 
  // chrome error!
  console.log.apply(console, arguments);
}
```

 

在使用console.log.apply调用的时候，上下文如果不传console,在chrome中会报错，囧。