Maison > interface Web > js tutoriel > le corps du texte

Pourquoi window.console.log est-il uniquement disponible lorsque les outils de développement sont ouverts dans IE9 ?

Mary-Kate Olsen
Libérer: 2024-11-15 01:16:02
original
537 Les gens l'ont consulté

Why is window.console.log Only Available When Developer Tools are Open in IE9?

IE9's Elusive Console.log: A Conditional Presence

In Internet Explorer 9, the existence of window.console.log presents an intriguing riddle. This article delves into the enigmatic nature of this function and explores the circumstances that govern its presence and behavior.

Window.console.log in IE9: An Incomplete Picture

Within IE9's strict confines, the window.console.log function is only granted access to the global scope if the developer tools curtain is gracefully parted for a given tab. However, this transient manifestation is not static. As you navigate the boundless web, the console object remains a constant companion, provided your developer tools window is not unceremoniously dismissed. But alas, a new tab demands a fresh pact with the developer tools to unveil the elusive console.

Why No Harmony with apply and call?

The riddle deepens when we consider the peculiar absence of window.console.log.apply and window.console.log.call. This discord stems from the console object's unique nature as a host object, one that proudly chooses not to inherit from Object or derive its methods from Function like its ECMAScript counterparts. Alas, IE9's imperfect embrace of DOM object conformity left the developer tools in a state of isolation, robbed of the same blessings bestowed upon their companions.

A Ray of Hope: Bind to the Rescue

Despite these limitations, a glimmer of hope emerges from the depths of Function.prototype.bind. This magical incantation empowers us to forge a harmonious bond between console methods and Function.prototype methods. Behold the following trickery:

var log = Function.prototype.bind.call(console.log, console);
log.apply(console, ["this", "is", "a", "test"]); //-> "thisisatest"
Copier après la connexion

And there you have it, the art of invoking console methods with a dash of Function.prototype sorcery.

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Derniers articles par auteur
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal