Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply window.console instead of console, which is no longer defined #3686

Merged
merged 2 commits into from
Oct 18, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions src/js/utils/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,6 @@ let log;
*/
export const logByType = (type, args, stringify = !!IE_VERSION && IE_VERSION < 11) => {

// If there's no console then don't try to output messages, but they will
// still be stored in `log.history`.
//
// Was setting these once outside of this function, but containing them
// in the function makes it easier to test cases where console doesn't exist
// when the module is executed.
const fn = window.console && window.console[type] || function() {};

if (type !== 'log') {

// add the type to the front of the message when it's not "log"
Expand All @@ -39,6 +31,19 @@ export const logByType = (type, args, stringify = !!IE_VERSION && IE_VERSION < 1
// add console prefix after adding to history
args.unshift('VIDEOJS:');

// If there's no console then don't try to output messages, but they will
// still be stored in `log.history`.
//
// Was setting these once outside of this function, but containing them
// in the function makes it easier to test cases where console doesn't exist
// when the module is executed.
const fn = window.console && window.console[type];

// Bail out if there's no console.
if (!fn) {
return;
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At this point, we've already stored the arguments in history; so, we can safely bail out if there's nothing to do. This is cheaper than potentially running JSON.stringify and calling a no-op.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds like a better idea

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also has the benefit of avoiding the previous potentiality of having a no-op assigned to fn when console does not exist then hitting line 70 and calling fn.apply(window.console...), which could cause an error (not entirely sure).


// IEs previous to 11 log objects uselessly as "[object Object]"; so, JSONify
// objects and arrays for those less-capable browsers.
if (stringify) {
Expand All @@ -62,7 +67,7 @@ export const logByType = (type, args, stringify = !!IE_VERSION && IE_VERSION < 1
if (!fn.apply) {
fn(args);
} else {
fn[Array.isArray(args) ? 'apply' : 'call'](console, args);
fn[Array.isArray(args) ? 'apply' : 'call'](window.console, args);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume using global console caused issues as well? and what about just calling apply and making args an array be default?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, the implied global console combined with the noop (which had apply()) was causing the problem in the first place.

As to the other change, I think I want to keep this fix pretty minimal. More changes are more opportunities for bugs!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

}
};

Expand Down