Javascript ÖÐ console µÄÓ÷¨

µ¼Óï console¶ÔÏóÊÇJavaScriptµÄÔ­Éú¶ÔÏó£¬ËüÓеãÏñUnixϵͳµÄ±ê×¼Êä³östdoutºÍ±ê×¼´íÎóstderr£¬¿ÉÒÔÊä³ö¸÷ÖÖÐÅÏ¢ÓÃÀ´µ÷ÊÔ³ÌÐò£¬¶øÇÒ»¹ÌṩÁ˺ܶà¶îÍâµÄ·½·¨£¬¹©¿ª·¢Õßµ÷Óá£ËüµÄ³£¼ûÓÃ;ÓÐÁ½¸ö¡£ÏÔʾÍøÒ³´úÂëÔËÐÐʱµÄ
console¶ÔÏóÊÇJavaScriptµÄÔ­Éú¶ÔÏó£¬ËüÓеãÏñUnixϵͳµÄ±ê×¼Êä³östdoutºÍ±ê×¼´íÎóstderr£¬¿ÉÒÔÊä³ö¸÷ÖÖÐÅÏ¢ÓÃÀ´µ÷ÊÔ³ÌÐò£¬¶øÇÒ»¹ÌṩÁ˺ܶà¶îÍâµÄ·½·¨£¬¹©¿ª·¢Õßµ÷Óá£ËüµÄ³£¼ûÓÃ;ÓÐÁ½¸ö¡£
  • ÏÔʾÍøÒ³´úÂëÔËÐÐʱµÄ´íÎóÐÅÏ¢¡£

  • ÌṩÁËÒ»¸öÃüÁîÐнӿڣ¬ÓÃÀ´ÓëÍøÒ³´úÂ뻥¶¯¡£

ä¯ÀÀÆ÷ʵÏÖ

console¶ÔÏóµÄä¯ÀÀÆ÷ʵÏÖ£¬°üº¬ÔÚä¯ÀÀÆ÷×Ô´øµÄ¿ª·¢¹¤¾ßÖ®ÖС£ÒÔChromeä¯ÀÀÆ÷µÄ“¿ª·¢Õß¹¤¾ß”£¨Developer Tools£©ÎªÀý£¬Ê×ÏÈʹÓÃÏÂÃæÈýÖÖ·½·¨µÄÒ»ÖÖ´ò¿ªËü¡£

  1. °´F12»òÕßControl+Shift+i£¨PCƽ̨£©/ Alt+Command+i£¨Macƽ̨£©¡£

  2. Ôڲ˵¥ÖÐÑ¡Ôñ“¹¤¾ß/¿ª·¢Õß¹¤¾ß”¡£

  3. ÔÚÒ»¸öÒ³ÃæÔªËØÉÏ£¬´ò¿ªÓÒ¼ü²Ëµ¥£¬Ñ¡ÔñÆäÖеēInspect Element”¡£

¿ª·¢Õß¹¤¾ß

´ò¿ª“¿ª·¢Õß¹¤¾ß”ÒԺ󣬿ÉÒÔ¿´µ½ÔÚ¶¥¶ËÓа˸öÃæ°å¿¨¿É¹©Ñ¡Ôñ£¬·Ö±ðÊÇ£º

  • Elements£ºÓÃÀ´µ÷ÊÔÍøÒ³µÄHTMLÔ´ÂëºÍCSS´úÂë¡£

  • Resources£º²é¿´ÍøÒ³¼ÓÔصĸ÷ÖÖ×ÊÔ´Îļþ£¨±ÈÈç´úÂëÎļþ¡¢×ÖÌåÎļþ¡¢cssÎļþµÈ£©£¬ÒÔ¼°ÔÚÓ²ÅÌÉÏ´´½¨µÄ¸÷ÖÖÄÚÈÝ£¨±ÈÈç±¾µØ»º´æ¡¢Cookie¡¢Local StorageµÈ£©¡£

  • Network£º²é¿´ÍøÒ³µÄHTTPͨÐÅÇé¿ö¡£

  • Sources£ºµ÷ÊÔJavaScript´úÂë¡£

  • Timeline£º²é¿´¸÷ÖÖÍøÒ³ÐÐΪËæʱ¼ä±ä»¯µÄÇé¿ö¡£

  • Profiles£º²é¿´ÍøÒ³µÄÐÔÄÜÇé¿ö£¬±ÈÈçCPUºÍÄÚ´æÏûºÄ¡£

  • Audits£ºÌṩÍøÒ³ÓÅ»¯µÄ½¨Òé¡£

  • Console£ºÓÃÀ´ÔËÐÐJavaScriptÃüÁî¡£

Õâ°Ë¸öÃæ°å¶¼Óи÷×ÔµÄÓÃ;£¬ÒÔÏÂÄÚÈÝÕë¶ÔConsoleÃæ°å£¬ÓÖ³ÆΪ¿ØÖÆ̨¡£ConsoleÃæ°å»ù±¾ÉϾÍÊÇÒ»¸öÃüÁîÐд°¿Ú£¬Äã¿ÉÒÔÔÚÌáʾ·ûÏ£¬¼üÈë¸÷ÖÖÃüÁî¡£

ÏÂÃæ½éÉÜconsole¶ÔÏóÌṩµÄ¸÷ÖÖ·½·¨¡£

console¶ÔÏóµÄ·½·¨

log()£¬info()£¬debug()

console.log·½·¨ÓÃÓÚÔÚconsole´°¿ÚÊä³öÐÅÏ¢¡£Ëü¿ÉÒÔ½ÓÊܶà¸ö²ÎÊý£¬½«ËüÃǵĽá¹ûÁ¬½ÓÆðÀ´Êä³ö¡£

console.log("Hello World")
// Hello World

console.log("a","b","c")
// a b c

console.log·½·¨»á×Ô¶¯ÔÚÿ´ÎÊä³öµÄ½á⣬Ìí¼Ó»»Ðзû¡£

console.log(1);
console.log(2);
console.log(3);
// 1
// 2
// 3

Èç¹ûµÚÒ»¸ö²ÎÊýÊǸñʽ×Ö·û´®£¨Ê¹ÓÃÁ˸ñʽռλ·û£©£¬console.log·½·¨½«ÒÀ´ÎÓúóÃæµÄ²ÎÊýÌ滻ռλ·û£¬È»ºóÔÙ½øÐÐÊä³ö¡£

console.log(" %s + %s = %s", 1, 1, 2)
//  1 + 1 = 2

ÉÏÃæ´úÂëÖУ¬console.log·½·¨µÄµÚÒ»¸ö²ÎÊýÓÐÈý¸öռλ·û£¨%s£©£¬µÚ¶þ¡¢Èý¡¢Ëĸö²ÎÊý»áÔÚÏÔʾʱ£¬ÒÀ´ÎÌæ»»µôÕâ¸öÈý¸öռλ·û¡£console.log·½·¨Ö§³ÖµÄռλ·ûÓÐÒÔϼ¸¸ö£¬²»Í¬¸ñʽµÄÊý¾Ý±ØÐëʹÓöÔÓ¦¸ñʽµÄռλ·û¡£

  • %s ×Ö·û´®
  • %d ÕûÊý
  • %i ÕûÊý
  • %f ¸¡µãÊý
  • %o ¶ÔÏóµÄÁ´½Ó
  • %c CSS¸ñʽ×Ö·û´®
var number = 11 * 9;
var color = 'red';

console.log('%d %s balloons', number, color);
// 99 red balloons

ÉÏÃæ´úÂëÖУ¬µÚ¶þ¸ö²ÎÊýÊÇÊýÖµ£¬¶ÔÓ¦µÄռλ·ûÊÇ%d£¬µÚÈý¸ö²ÎÊýÊÇ×Ö·û´®£¬¶ÔÓ¦µÄռλ·ûÊÇ%s¡£

ʹÓÃ%cռλ·ûʱ£¬¶ÔÓ¦µÄ²ÎÊý±ØÐëÊÇCSSÓï¾ä£¬ÓÃÀ´¶ÔÊä³öÄÚÈݽøÐÐCSSäÖȾ¡£

console.log('%cThis text is styled!',
  'color: #86CC00; background-color: blue; font-size: 20px; padding: 3px;'
)

ÉÏÃæ´úÂëÔËÐкó£¬Êä³öµÄÄÚÈݽ«ÏÔʾΪÀ¶µ×ÂÌ×Ö¡£

log·½·¨µÄÁ½ÖÖ²ÎÊý¸ñʽ£¬¿ÉÒÔ½áºÏÔÚÒ»ÆðʹÓá£

console.log(" %s + %s ", 1, 1, "= 2")
// 1 + 1  = 2

Èç¹û²ÎÊýÊÇÒ»¸ö¶ÔÏó£¬console.log»áÏÔʾ¸Ã¶ÔÏóµÄÖµ¡£

console.log({foo: 'bar'})
// Object {foo: "bar"}

console.log(Date)
// function Date() { [native code] }

ÉÏÃæ´úÂëÊä³öDate¶ÔÏóµÄÖµ£¬½á¹ûΪһ¸ö¹¹Ô캯Êý¡£

console.info()ºÍconsole.debug()¶¼ÊÇconsole.log·½·¨µÄ±ðÃû£¬Ó÷¨ÍêÈ«Ò»Ñù¡£console.info·½·¨»áÔÚÊä³öÐÅÏ¢µÄÇ°Ã棬¼ÓÉÏÒ»¸öÀ¶É«Í¼±ê¡£

console¶ÔÏóµÄËùÓз½·¨£¬¶¼¿ÉÒÔ±»¸²¸Ç¡£Òò´Ë£¬¿ÉÒÔ°´ÕÕ×Ô¼ºµÄÐèÒª£¬¶¨Òåconsole.log·½·¨¡£

["log", "info", "warn", "error"].forEach(function(method) {
    console[method] = console[method].bind(
        console,
        new Date().toISOString()
    );
});

console.log("³ö´íÁË£¡");
// 2014-05-18T09:00.000Z ³ö´íÁË£¡

ÉÏÃæ´úÂë±íʾ£¬Ê¹ÓÃ×Ô¶¨ÒåµÄconsole.log·½·¨£¬¿ÉÒÔÔÚÏÔʾ½á¹ûÌí¼Óµ±Ç°Ê±¼ä¡£

warn()£¬error()

warn·½·¨ºÍerror·½·¨Ò²ÊÇÊä³öÐÅÏ¢£¬ËüÃÇÓëlog·½·¨µÄ²»Í¬Ö®´¦ÔÚÓÚ£¬warn·½·¨Êä³öÐÅϢʱ£¬ÔÚ×îÇ°Ãæ¼ÓÒ»¸ö»ÆÉ«Èý½Ç£¬±íʾ¾¯¸æ£»error·½·¨Êä³öÐÅϢʱ£¬ÔÚ×îÇ°Ãæ¼ÓÒ»¸öºìÉ«µÄ²æ£¬±íʾ³ö´í£¬Í¬Ê±»áÏÔʾ´íÎó·¢ÉúµÄ¶ÑÕ»¡£ÆäËûÓ÷¨¶¼Ò»Ñù¡£

console.error("Error: %s (%i)", "Server is not responding",500)
// Error: Server is not responding (500)

console.warn('Warning! Too few nodes (%d)', document.childNodes.length)
// Warning! Too few nodes (1)

±¾ÖÊÉÏ£¬log·½·¨ÊÇдÈë±ê×¼Êä³ö£¨stdout£©£¬warn·½·¨ºÍerror·½·¨ÊÇдÈë±ê×¼´íÎó£¨stderr£©¡£

table()

¶ÔÓÚijЩ¸´ºÏÀàÐ͵ÄÊý¾Ý£¬console.table·½·¨¿ÉÒÔ½«ÆäתΪ±í¸ñÏÔʾ¡£

var languages = [
  { name: "JavaScript", fileExtension: ".js" },
  { name: "TypeScript", fileExtension: ".ts" },
  { name: "CoffeeScript", fileExtension: ".coffee" }
];

console.table(languages);

ÉÏÃæ´úÂëµÄlanguage£¬×ªÎª±í¸ñÏÔʾÈçÏ¡£

(index) name fileExtension
0 "JavaScript" ".js"
1 "TypeScript" ".ts"
2 "CoffeeScript" ".coffee"

¸´ºÏÐÍÊý¾ÝתΪ±í¸ñÏÔʾµÄÌõ¼þÊÇ£¬±ØÐëÓµÓÐÖ÷¼ü¡£¶ÔÓÚÉÏÃæµÄÊý×éÀ´Ëµ£¬Ö÷¼ü¾ÍÊÇÊý×Ö¼ü¡£¶ÔÓÚ¶ÔÏóÀ´Ëµ£¬Ö÷¼ü¾ÍÊÇËüµÄ×îÍâ²ã¼ü¡£

var languages = {
    csharp: { name: "C#", paradigm: "object-oriented" },
    fsharp: { name: "F#", paradigm: "functional" }
};

console.table(languages);

ÉÏÃæ´úÂëµÄlanguage£¬×ªÎª±í¸ñÏÔʾÈçÏ¡£

(index) name paradigm
csharp "C#" "object-oriented"
fsharp "F#" "functional"

count()

count·½·¨ÓÃÓÚ¼ÆÊý£¬Êä³öËü±»µ÷ÓÃÁ˶àÉٴΡ£

function greet(user) {
  console.count();
  return "hi " + user;
}

greet('bob')
//  : 1
// "hi bob"

greet('alice')
//  : 2
// "hi alice"

greet('bob')
//  : 3
// "hi bob"

ÉÏÃæ´úÂëÿ´Îµ÷ÓÃgreetº¯Êý£¬ÄÚ²¿µÄconsole.count·½·¨¾ÍÊä³öÖ´ÐдÎÊý¡£

¸Ã·½·¨¿ÉÒÔ½ÓÊÜÒ»¸ö×Ö·û´®×÷Ϊ²ÎÊý£¬×÷Ϊ±êÇ©£¬¶ÔÖ´ÐдÎÊý½øÐзÖÀà¡£

function greet(user) {
  console.count(user);
  return "hi " + user;
}

greet('bob')
// bob: 1
// "hi bob"

greet('alice')
// alice: 1
// "hi alice"

greet('bob')
// bob: 2
// "hi bob"

ÉÏÃæ´úÂë¸ù¾Ý²ÎÊýµÄ²»Í¬£¬ÏÔʾbobÖ´ÐÐÁËÁ½´Î£¬aliceÖ´ÐÐÁËÒ»´Î¡£

dir()

dir·½·¨ÓÃÀ´¶ÔÒ»¸ö¶ÔÏó½øÐмì²é£¨inspect£©£¬²¢ÒÔÒ×ÓÚÔĶÁºÍ´òÓ¡µÄ¸ñʽÏÔʾ¡£

console.log({f1: 'foo', f2: 'bar'})
// Object {f1: "foo", f2: "bar"}

console.dir({f1: 'foo', f2: 'bar'})
// Object
//   f1: "foo"
//   f2: "bar"
//   __proto__: Object

ÉÏÃæ´úÂëÏÔʾdir·½·¨µÄÊä³ö½á¹û£¬±Èlog·½·¨¸üÒ׶Á£¬ÐÅÏ¢Ò²¸ü·á¸»¡£

¸Ã·½·¨¶ÔÓÚÊä³öDOM¶ÔÏó·Ç³£ÓÐÓã¬ÒòΪ»áÏÔʾDOM¶ÔÏóµÄËùÓÐÊôÐÔ¡£

console.dir(document.body)

assert()

assert·½·¨½ÓÊÜÁ½¸ö²ÎÊý£¬µÚÒ»¸ö²ÎÊýÊDZí´ïʽ£¬µÚ¶þ¸ö²ÎÊýÊÇ×Ö·û´®¡£Ö»Óе±µÚÒ»¸ö²ÎÊýΪfalse£¬²Å»áÊä³öµÚ¶þ¸ö²ÎÊý£¬·ñÔò²»»áÓÐÈκνá¹û¡£

// ʵÀý
console.assert(true === false, "ÅжÏÌõ¼þ²»³ÉÁ¢")
// Assertion failed: ÅжÏÌõ¼þ²»³ÉÁ¢

ÏÂÃæÊÇÁíÒ»¸öÀý×Ó£¬ÅжÏ×Ó½ÚµãµÄ¸öÊýÊÇ·ñ´óÓÚµÈÓÚ500¡£

console.assert(list.childNodes.length < 500, "½Úµã¸öÊý´óÓÚµÈÓÚ500")

time()£¬timeEnd()

ÕâÁ½¸ö·½·¨ÓÃÓÚ¼Æʱ£¬¿ÉÒÔËã³öÒ»¸ö²Ù×÷Ëù»¨·ÑµÄ׼ȷʱ¼ä¡£

console.time("Array initialize");

var array= new Array(1000000);
for (var i = array.length - 1; i >= 0; i--) {
    array[i] = new Object();
};

console.timeEnd("Array initialize");

// Array initialize: 1914.481ms

time·½·¨±íʾ¼Æʱ¿ªÊ¼£¬timeEnd·½·¨±íʾ¼Æʱ½áÊø¡£ËüÃǵIJÎÊýÊǼÆʱÆ÷µÄÃû³Æ¡£µ÷ÓÃtimeEnd·½·¨Ö®ºó£¬console´°¿Ú»áÏÔʾ“¼ÆʱÆ÷Ãû³Æ: ËùºÄ·ÑµÄʱ¼ä”¡£

timeline()£¬timelineEnd()£¬timeStamp()

console.timelineºÍconsole.timelineEndÕâÁ½¸ö·½·¨ÓÃÓÚ¶¨ÒåÒ»¸öеÄʱ¼äÏߣ¬¿ÉÒÔÔÚTimelineÃæ°å²é¿´¡£ÕâÁ½¸ö·½·¨Ö»ÓÐChrome¿ª·¢Õß¹¤¾ßÖ§³Ö¡£

console.timeline('Google Search');

// Do some work

console.timelineEnd('Google Search');

ÉÏÃæ´úÂ붨ÒåÁËÒ»¸öÃû³Æ½Ð×ö“Google Search”µÄʱ¼äÏߣ¬ÔÚÕâ¸öʱ¼äÏßÀïµÄËùÓÐʼþµÄºÄʱ£¬¿ÉÒÔÔÚTimelineÃæ°åÖв鿴¡£

console.timeStamp·½·¨ÓÃÔÚÉÏÃæÁ½¸ö·½·¨µÄÖм䣬ÓÃÓÚΪʱ¼äÏßÌí¼ÓÒ»¸öʱ¼ä´Á¡£Ê±¼ä´ÁµÄÃû×Ö¾ÍÊÇtimeStamp·½·¨µÄ²ÎÊý¡£

profile()£¬profileEnd()

console.profile·½·¨ÓÃÀ´Ð½¨Ò»¸öÐÔÄܲâÊÔÆ÷£¨profile£©£¬ËüµÄ²ÎÊýÊÇÐÔÄܲâÊÔÆ÷µÄÃû×Ö¡£

console.profile('p')
// Profile 'p' started.

console.profileEnd·½·¨ÓÃÀ´½áÊøÕýÔÚÔËÐеÄÐÔÄܲâÊÔÆ÷¡£

console.profileEnd()
// Profile 'p' finished.

´ò¿ªä¯ÀÀÆ÷µÄ¿ª·¢Õß¹¤¾ß£¬ÔÚprofileÃæ°åÖУ¬¿ÉÒÔ¿´µ½Õâ¸öÐÔÄܵ÷ÊÔÆ÷µÄÔËÐнá¹û¡£

group()£¬groupend()£¬groupCollapsed()

console.groupºÍconsole.groupendÕâÁ½¸ö·½·¨ÓÃÓÚ½«ÏÔʾµÄÐÅÏ¢·Ö×é¡£ËüÖ»ÔÚÊä³ö´óÁ¿ÐÅϢʱÓÐÓ㬷ÖÔÚÒ»×éµÄÐÅÏ¢£¬¿ÉÒÔÓÃÊó±êÕÛµþ/Õ¹¿ª¡£

console.group('Group One');
console.group('Group Two');

// some code

console.groupEnd(); // Group Two ½áÊø
console.groupEnd(); // Group One ½áÊø

console.groupCollapsed·½·¨Óëconsole.group·½·¨ºÜÀàËÆ£¬Î¨Ò»µÄÇø±ðÊǸÃ×éµÄÄÚÈÝ£¬ÔÚµÚÒ»´ÎÏÔʾʱÊÇÊÕÆðµÄ£¨collapsed£©£¬¶ø²»ÊÇÕ¹¿ªµÄ¡£

console.groupCollapsed('Fetching Data');

console.log('Request Sent');
console.error('Error: Server not responding (500)');

console.groupEnd();

ÉÏÃæ´úÂëÖ»ÏÔʾһÐДFetching Data“£¬µã»÷ºó²Å»áÕ¹¿ª£¬ÏÔʾÆäÖаüº¬µÄÁ½ÐС£

trace()£¬clear()

console.trace·½·¨ÏÔʾµ±Ç°Ö´ÐеĴúÂëÔÚ¶ÑÕ»Öеĵ÷Ó÷¾¶¡£

console.trace()
// console.trace()
//   (anonymous function)
//   InjectedScript._evaluateOn
//   InjectedScript._evaluateAndWrap
//   InjectedScript.evaluate

console.clear·½·¨ÓÃÓÚÇå³ýµ±Ç°¿ØÖÆ̨µÄËùÓÐÊä³ö£¬½«¹â±ê»ØÖõ½µÚÒ»ÐС£

ÃüÁîÐÐAPI

ÔÚ¿ØÖÆ̨ÖУ¬³ýÁËʹÓÃconsole¶ÔÏ󣬻¹¿ÉÒÔʹÓÃһЩ¿ØÖÆ̨×Ô´øµÄÃüÁîÐз½·¨¡£

£¨1£©$_

$_ÊôÐÔ·µ»ØÉÏÒ»¸ö±í´ïʽµÄÖµ¡£

2+2
// 4
$_
// 4

£¨2£©$0 - $4

¿ØÖÆ̨±£´æÁË×î½ü5¸öÔÚElementsÃæ°åÑ¡ÖеÄDOMÔªËØ£¬$0´ú±íµ¹ÊýµÚÒ»¸ö£¬$1´ú±íµ¹ÊýµÚ¶þ¸ö£¬ÒÔ´ËÀàÍÆÖ±µ½$4¡£

£¨3£©$(selector)

$(selector)·µ»ØÒ»¸öÊý×飬°üÀ¨Ìض¨µÄCSSÑ¡ÔñÆ÷Æ¥ÅäµÄËùÓÐDOMÔªËØ¡£¸Ã·½·¨Êµ¼ÊÉÏÊÇdocument.querySelectorAll·½·¨µÄ±ðÃû¡£

var images = $('img');
for (each in images) {
    console.log(images[each].src);
}

ÉÏÃæ´úÂë´òÓ¡³öÍøÒ³ÖÐËùÓÐimgÔªËصÄsrcÊôÐÔ¡£

£¨4£©$$(selector)

$$(selector)·µ»ØÒ»¸öÑ¡ÖеÄDOM¶ÔÏ󣬵ÈͬÓÚdocument.querySelectorAll¡£

£¨5£©$x(path)

$x(path)·½·¨·µ»ØÒ»¸öÊý×飬°üº¬Æ¥ÅäÌض¨XPath±í´ïʽµÄËùÓÐDOMÔªËØ¡£

$x("//p[a]")

ÉÏÃæ´úÂë·µ»ØËùÓаüº¬aÔªËصÄpÔªËØ¡£

£¨6£©inspect(object)

inspect(object)·½·¨´ò¿ªÏà¹ØÃæ°å£¬²¢Ñ¡ÖÐÏàÓ¦µÄÔªËØ£ºDOMÔªËØÔÚElementsÃæ°åÖÐÏÔʾ£¬JavaScript¶ÔÏóÔÚProfilesÖÐÏÔʾ¡£

£¨7£©getEventListeners(object)

getEventListeners(object)·½·¨·µ»ØÒ»¸ö¶ÔÏ󣬸öÔÏóµÄ³ÉԱΪµÇ¼ÇÁ˻ص÷º¯ÊýµÄ¸÷ÖÖʼþ£¨±ÈÈçclick»òkeydown£©£¬Ã¿¸öʼþ¶ÔÓ¦Ò»¸öÊý×飬Êý×éµÄ³ÉԱΪ¸ÃʼþµÄ»Øµ÷º¯Êý¡£

£¨8£©keys(object)£¬values(object)

keys(object)·½·¨·µ»ØÒ»¸öÊý×飬°üº¬Ìض¨¶ÔÏóµÄËùÓмüÃû¡£

values(object)·½·¨·µ»ØÒ»¸öÊý×飬°üº¬Ìض¨¶ÔÏóµÄËùÓмüÖµ¡£

var o = {'p1':'a', 'p2':'b'};

keys(o)
// ["p1", "p2"]
values(o)
// ["a", "b"]

£¨9£©monitorEvents(object[, events]) £¬unmonitorEvents(object[, events])

monitorEvents(object[, events])·½·¨¼àÌýÌض¨¶ÔÏóÉÏ·¢ÉúµÄÌض¨Ê¼þ¡£µ±ÕâÖÖÇé¿ö·¢Éúʱ£¬»á·µ»ØÒ»¸öEvent¶ÔÏ󣬰üº¬¸ÃʼþµÄÏà¹ØÐÅÏ¢¡£unmonitorEvents·½·¨ÓÃÓÚÍ£Ö¹¼àÌý¡£

monitorEvents(window, "resize");

monitorEvents(window, ["resize", "scroll"])

ÉÏÃæ´úÂë·Ö±ð±íʾµ¥¸öʼþºÍ¶à¸öʼþµÄ¼àÌý·½·¨¡£

monitorEvents($0, "mouse");
unmonitorEvents($0, "mousemove");

ÉÏÃæ´úÂë±íʾÈçºÎÍ£Ö¹¼àÌý¡£

monitorEventsÔÊÐí¼àÌýͬһ´óÀàµÄʼþ¡£ËùÓÐʼþ¿ÉÒÔ·Ö³ÉËĸö´óÀà¡£

  • mouse£º"mousedown", "mouseup", "click", "dblclick", "mousemove", "mouseover", "mouseout", "mousewheel"
  • key£º"keydown", "keyup", "keypress", "textInput"
  • touch£º"touchstart", "touchmove", "touchend", "touchcancel"
  • control£º"resize", "scroll", "zoom", "focus", "blur", "select", "change", "submit", "reset"
monitorEvents($("#msg"), "key");

ÉÏÃæ´úÂë±íʾ¼àÌýËùÓÐkey´óÀàµÄʼþ¡£

£¨10£©profile([name])£¬profileEnd()

profile·½·¨ÓÃÓÚÆô¶¯Ò»¸öÌض¨Ãû³ÆµÄCPUÐÔÄܲâÊÔ£¬profileEnd·½·¨ÓÃÓÚ½áÊø¸ÃÐÔÄܲâÊÔ¡£

profile("My profile")

profileEnd("My profile")

£¨11£©ÆäËû·½·¨

ÃüÁîÐÐAPI»¹ÌṩÒÔÏ·½·¨¡£

  • clear()·½·¨Çå³ý¿ØÖÆ̨µÄÀúÊ·¡£
  • copy(object)·½·¨¸´ÖÆÌض¨DOMÔªËص½¼ôÌù°å¡£
  • dir(object)·½·¨ÏÔʾÌض¨¶ÔÏóµÄËùÓÐÊôÐÔ£¬ÊÇconsole.dir·½·¨µÄ±ðÃû¡£
  • dirxml(object)·½·¨ÏÔʾÌض¨¶ÔÏóµÄXMLÐÎʽ£¬ÊÇconsole.dirxml·½·¨µÄ±ðÃû¡£

debuggerÓï¾ä

debuggerÓï¾ä±ØÐëÓë³ý´í¹¤¾ßÅäºÏʹÓã¬Èç¹ûûÓгý´í¹¤¾ß£¬debuggerÓï¾ä²»»á²úÉúÈκνá¹û¡£

ÔÚchromeä¯ÀÀÆ÷ÖУ¬µ±´úÂëÔËÐе½debuggerÖ¸¶¨µÄÐÐʱ£¬¾Í»áÔÝÍ£ÔËÐУ¬×Ô¶¯´ò¿ªconsole½çÃæ¡£ËüµÄ×÷ÓÃÀàËÆÓÚÉèÖöϵ㡣

for(var i = 0;i<5;i++){
    console.log(i);
    if (i===2) debugger;
}

ÉÏÃæ´úÂë´òÓ¡³ö0£¬1£¬2ÒԺ󣬾ͻáÔÝÍ££¬×Ô¶¯´ò¿ªconsole´°¿Ú£¬µÈ´ý½øÒ»²½´¦Àí¡£

Òƶ¯¶Ë¿ª·¢

£¨±¾½ÚÔÝ´æ´Ë´¦£©

Ä£ÄâÊÖ»úÊÓ¿Ú£¨viewport£©

chromeä¯ÀÀÆ÷µÄ¿ª·¢Õß¹¤¾ß£¬Ìṩһ¸öÑ¡Ï¿ÉÒÔÄ£ÄâÊÖ»úÆÁÄ»µÄÏÔʾЧ¹û¡£

´ò¿ª“ÉèÖÔµÄOverridesÃæ°å£¬Ñ¡ÔñÏàÓ¦µÄUser AgentºÍDevice MetricsÑ¡Ïî¡£

Ñ¡ÔñUser Agent

User Agent¿ÉÒÔʹµÃµ±Ç°ä¯ÀÀÆ÷·¢³öÊÖ»úä¯ÀÀÆ÷µÄAgent×Ö·û´®£¬Device MetricsÔòʹµÃµ±Ç°ä¯ÀÀÆ÷µÄÊÓ¿Ú±äΪÊÖ»úµÄÊÓ¿Ú´óС¡£ÕâÁ½¸öÑ¡Ïî¿ÉÒÔ¶ÀÁ¢Ñ¡Ôñ£¬²»Ò»¶¨Í¬Ê±Ñ¡ÖС£

Ä£Äâtouchʼþ

ÎÒÃÇ¿ÉÒÔÔÚPC¶ËÄ£ÄâJavaScriptµÄtouchʼþ¡£

Ê×ÏÈ£¬´ò¿ªchromeä¯ÀÀÆ÷µÄ¿ª·¢Õß¹¤¾ß£¬Ñ¡Ôñ“ÉèÖÔÖеÄOverridesÃæ°å£¬¹´Ñ¡“Enable touch events”Ñ¡Ïî¡£

Enable touch eventsµÄͼƬ

È»ºó£¬Êó±ê¾Í»á´¥·¢touchstart¡¢touchmoveºÍtouchendʼþ¡££¨´Ëʱ£¬Êó±ê±¾ÉíµÄʼþÒÀÈ»ÓÐЧ¡££©

ÖÁÓÚ¶àµã´¥Ãþ£¬±ØÐëÒªÓÐÖ§³ÖÕâ¸ö¹¦ÄܵÄÉ豸²ÅÄÜÄ£Ä⣬¾ßÌå¿ÉÒԲο¼Multi-touch web development¡£

Ä£Ä⾭γ¶È

chromeä¯ÀÀÆ÷µÄ¿ª·¢Õß¹¤¾ß£¬»¹¿ÉÒÔÄ£Ä⵱ǰµÄ¾­Î³¶ÈÊý¾Ý¡£´ò¿ª“ÉèÖÔµÄOverridesÃæ°å£¬Ñ¡ÖÐOverride GeolocationÑ¡Ï²¢ÌîÈëÏàÓ¦¾­¶ÈºÍγ¶ÈÊý¾Ý¡£

Ä£Ä⾭γ¶È

Ô¶³Ì³ý´í

(1) Chrome for Android

AndroidÉ豸ÉϵÄChromeä¯ÀÀÆ÷Ö§³ÖUSB³ý´í¡£PC¶ËÐèÒª°²×°Android SDKºÍChromeä¯ÀÀÆ÷£¬È»ºóÓÃusbÏß½«ÊÖ»úºÍPCÁ¬ÆðÀ´£¬¿É²Î¿¼¹Ù·½Îĵµ¡£

(2) Opera

Operaä¯ÀÀÆ÷µÄ³ý´í»·¾³DragonflyÖ§³ÖÔ¶³Ì³ý´í£¨½Ì³Ì£©¡£

(3) Firefox for Android

²Î¿¼¹Ù·½Îĵµ¡£

(4) Safari on iOS6

Äã¿ÉÒÔʹÓÃMac×ÀÃæµçÄÔµÄSafari 6ä¯ÀÀÆ÷£¬½øÐÐÔ¶³Ì³ý´í£¨½Ì³Ì£©¡£

Google Closure

£¨±¾½ÚÔÝ´æ´Ë´¦£©

Google ClosureÊÇGoogleÌṩµÄÒ»¸öJavaScriptÔ´Âë´¦Àí¹¤¾ß£¬Ö÷ÒªÓÃÓÚѹËõºÍºÏ²¢¶à¸öJavaScript½Å±¾Îļþ¡£

Google ClosureʹÓÃJavaÓïÑÔ¿ª·¢£¬Ê¹ÓÃ֮ǰ±ØÐëÏÈ°²×°Java¡£È»ºó£¬Ç°Íù¹Ù·½ÍøÕ¾½øÐÐÏÂÔØ£¬ÕâÀïÎÒÃÇÖ÷ҪʹÓÃÆäÖеıàÒëÆ÷£¨compiler£©¡£

Ê×ÏÈ£¬²é¿´Ê¹ÓðïÖú¡£

java -jar /path/to/closure/compiler.jar --help

Ö±½ÓÔڽű¾ÃüÁîºóÃæ¸úÉÏÒªºÏ²¢µÄ½Å±¾£¬¾ÍÄÜÍê³ÉºÏ²¢¡£

java -jar /path/to/closure/compiler.jar *.js

ʹÓÃ--js²ÎÊý£¬¿ÉÒÔÈ·±£°´ÕÕ²ÎÊýµÄÏȺó´ÎÐòºÏ²¢Îļþ¡£

java -jar /path/to/closure/compiler.jar --js script1.js --js script2.js --js script3.js

µ«ÊÇ£¬ÕâÑùµÄÔËÐнá¹ûÊǽ«ºÏ²¢ºóµÄÎļþÈ«²¿Êä³öµ½ÆÁÄ»£¨±ê×¼Êä³ö£©£¬Òò´ËÐèҪʹÓÃ--js_output_file²ÎÊý£¬Ö¸¶¨ºÏ²¢ºóµÄÎļþÃû¡£

java -jar /path/to/closure/compiler.jar --js script1.js --js script2.js --js script3.js --js_output_file scripts-compiled.js

(±¾½ÚÔÝ´æ´Ë´¦)

µÚÒ»ÖÖ×ö·¨

×î³£¼ûµÄ²âÊÔÐÔÄܵÄ×ö·¨£¬¾ÍÊÇͬһ²Ù×÷Öظ´n´Î£¬È»ºó¼ÆËãÿ´Î²Ù×÷µÄƽ¾ùʱ¼ä¡£

var totalTime,
    start = new Date,
    iterations = 6;

while (iterations--) {
  // Code snippet goes here
}

// totalTime → the number of milliseconds it took to execute
// the code snippet 6 times
totalTime = new Date - start;

ÉÏÃæ´úÂëµÄÎÊÌâÔÚÓÚ£¬ÓÉÓÚ¼ÆËã»úµÄÐÔÄܲ»¶ÏÌá¸ß£¬Èç¹ûÖ»Öظ´6´Î£¬ºÜ¿ÉÄܵõ½0ºÁÃëµÄ½á¹û£¬¼´²»µ½1ºÁÃ룬JavascriptÒýÇæÎÞ·¨²âÁ¿¡£

µÚ¶þÖÖ×ö·¨

ÁíÒ»ÖÖ˼·ÊÇ£¬²âÊÔµ¥Î»Ê±¼äÄÚÍê³ÉÁ˶àÉٴβÙ×÷¡£

var hz,
    period,
    startTime = new Date,
    runs = 0;

do {
  // Code snippet goes here
  runs++;
  totalTime = new Date - startTime;
} while (totalTime < 1000);

// convert ms to seconds
totalTime /= 1000;

// period → how long per operation
period = totalTime / runs;

// hz → the number of operations per second
hz = 1 / period;

// can be shortened to
// hz = (runs * 1000) / totalTime;

ÕâÖÖ×ö·¨µÄ×¢ÒâÖ®´¦ÔÚÓÚ£¬²âÊԽṹÊÜÍâ½ç»·¾³Ó°ÏìºÜ´ó£¬ÎªÁ˵õ½ÕýÈ·½á¹¹£¬±ØÐëÖظ´¶à´Î¡£

Ô­ÎĵØÖ·£ºhttp://javascript.ruanyifeng.com/tool/console.html

http://www.aseoe.com/ true Javascript ÖÐ console µÄÓ÷¨ http://www.aseoe.com/show-12-684-1.html report <£¿php echo strlen($content) / 2; ?> console¶ÔÏóÊÇJavaScriptµÄÔ­Éú¶ÔÏó£¬ËüÓеãÏñUnixϵͳµÄ±ê×¼Êä³östdoutºÍ±ê×¼´íÎóstderr£¬¿ÉÒÔÊä³ö¸÷ÖÖÐÅÏ¢ÓÃÀ´µ÷ÊÔ³ÌÐò£¬¶øÇÒ»¹ÌṩÁ˺ܶà¶îÍâµÄ·½·¨£¬¹©¿ª·¢Õßµ÷Óá£ËüµÄ³£¼ûÓÃ;ÓÐÁ½¸ö¡£ÏÔʾÍøÒ³´úÂëÔËÐÐʱµÄ
TAG:Javascript console
±¾Õ¾»¶Ó­ÈκÎÐÎʽµÄתÔØ£¬µ«ÇëÎñ±Ø×¢Ã÷³ö´¦£¬×ðÖØËûÈËÀͶ¯³É¹û
תÔØÇë×¢Ã÷£º ÎÄÕÂתÔØ×Ô£º°®Ë¼×ÊÔ´Íø http://www.aseoe.com/show-12-684-1.html

[Ç°¶Ë²å¼þÍƼö] Plugin

1 2 3 4
  • jQueryʵÏÖÖð×ÖÖð¾äÏÔʾ²å¼þl-by-l.min.js
  • jQuery´ø·½Ïò¸ÐÖªµÄÊó±ê»¬¹ýͼƬ±ß¿òÌØЧ²å¼þ
  • jQuery HotKeys¼àÌý¼üÅÌ°´ÏÂʼþkeydown²å¼þ
  • ÏìӦʽÎÞÏÞÂÖ²¥jQueryÐýתľÂí²å¼þ
ÏìӦʽÎÞÏÞÂÖ²¥jQueryÐýתľÂí²å¼þ
webÇ°¶Ë¿ª·¢
°®Ë¼×ÊÔ´Íø Copyright 2012-2014 Www.Aseoe.Com All rights reserved.(½úICP±¸13001436ºÅ-1)