President Trump reserves some of his worst behavior for foreign trips, such as abasing himself before President Vladimir V. Putin of Russia in Helsinki a year ago, skipping a ceremony in France last fall to honor American soldiers killed in World War I (too rainy, the White House said) and insulting the mayor of London earlier this month. Yet even by Mr. Trump’s dismal standards, his performance this week before the G-20 summit in Osaka, Japan, should take everyone’s breath away. More than yet another demonstration of his erratic behavior, this was also an object lesson in the dangers of his context-free hostility to the world beyond the United States.

Before arriving in Japan, Mr. Trump had reportedly been musing about withdrawing the United States from the security treaty with Japan signed in 1951 and revised in 1960 — the cornerstone of the alliance between the United States and Japan and a pillar of American foreign policy. On Wednesday, asked about the treaty on Fox News, Mr. Trump sneered, “If Japan is attacked, we will fight World War III.” Then he added: “But if we’re attacked, Japan doesn’t have to help us at all. They can watch it on a Sony television.”

Mr. Trump’s comment demonstrates a strategic cluelessness and historical ignorance that would disqualify a person from even a modest desk job at the State Department.

Though Mr. Trump implied that the security treaty favors Japan, it was largely dictated by the United States. After Imperial Japan surrendered to the Allies in August 1945, ending World War II, the country was placed under an American-led occupation overseen by the domineering Gen. Douglas MacArthur. When that occupation ended in April 1952, Japan had turned away from militarism to embrace ideals of pacifism and democracy. Under Article 9 of a new Constitution that was originally drafted in English at MacArthur’s headquarters, Japan renounced war and pledged never to maintain land, sea or air forces.