The scene was striking: an American president, standing before reporters on foreign soil, declaring that one of his potential successors had “rattled” other heads of state because he had displayed “either an ignorance of world affairs or a cavalier attitude” about them.

President Obama’s public disparagement Thursday of Donald Trump, the presumptive Republican nominee, during a news conference in Ise City, Japan, obliterated the now-quaint political convention that partisanship stops at the water’s edge. It also revealed a stark truth: The world is worried about Trump.

Although he is not on the November ticket, Obama has a foreign policy legacy to protect, particularly against Trump, who has called the president’s approach weak and incoherent.

[With his legacy in the balance, Obama works to boost Democrats in 2016]

The billionaire, who has openly mocked foreign dignitaries for questioning his capacity to serve as America’s leader, remained unapologetic in the face of Obama’s critique.

U.S. President Obama talks with Italian Prime Minister Matteo Renzi, left, as they arrive at the second working session during the 2016 Ise-Shima G-7 Summit in Shima, Japan, on May 26. (Carlos Barria/Pool photo via Associated Press)

“When you rattle someone, that’s good because many of the world, as you know, many of the countries in our world, our beautiful world, have been absolutely abusing us and taking advantage of us,” he said at a news conference in Bismarck, N.D. “So if they’re rattled, in a friendly way, we’re going to have great relationships with these countries.”

Several historians said it was rare, if not unprecedented, for an outgoing president to criticize the foreign policy position of someone from the opposite party by name while traveling abroad.

President Dwight D. Eisenhower thought John F. Kennedy was too inexperienced for the job, but he made only an oblique reference to this at the end of the campaign between Kennedy and Richard M. Nixon.“We need a leader who will not, one day, say that the United States government should intervene in Cuba and then retract it the next day,” Eisenhower said at the time.

Lyndon B. Johnson detested his 1964 opponent, Barry Goldwater, but refused to mention him by name while on the campaign trail. Instead, according to University of Utah history professor Robert A. Goldberg, he warned voters of having a “reckless” and “radical” leader with his hand on the nuclear trigger.

The closest an outgoing president has come to launching into a foreign policy critique of a possible successor while abroad was George W. Bush, when he spoke before the Israeli parliament in 2008.

“Some seem to believe that we should negotiate with the terrorists and radicals, as if some ingenious argument will persuade them they have been wrong all along,” Bush said, without naming anyone.

Obama has explicitly criticized Trump several times this year, saying the presidency requires sober judgment, and some leaders have asked him about Trump’s most controversial remarks. But he went even further at the close of this week’s G-7 summit. In response to a question, the president said foreign leaders are “surprised” by Trump and that they “are not sure how seriously to take some of his pronouncements.”

1 of 51 Full Screen Autoplay Close Skip Ad × What Obama is doing on his historic Asia trip View Photos The president arrived in Japan for the Group of Seven summit, where the leaders of the seven advanced economies are meeting for two days. The president will also visit Hiroshima, the Japanese city where the United States dropped an atomic bomb in 1945. Caption The president arrived in Japan for the Group of Seven summit, where the leaders of the seven advanced economies are meeting for two days. Obama also visited Hiroshima, the Japanese city where the United States dropped an atomic bomb in 1945. May 27, 2016 President Obama hugs atomic bomb survivor Shigeaki Mori as he visits Hiroshima Peace Memorial Park. Carlos Barria/Reuters Buy Photo Wait 1 second to continue.

“But they’re rattled by it — and for good reason — because a lot of the proposals that he’s made display either ignorance of world affairs or a cavalier attitude or an interest in getting tweets and headlines instead of actually thinking through what it is that is required to keep America safe and secure and prosperous, and what’s required to keep the world on an even keel,” he said.

Just four years ago, Obama chastised an adviser to his then-GOP rival Mitt Romney, Glenn Hubbard, for questioning his economic policies in the opinion pages of a German newspaper. “And I think, traditionally, the notion has been that America’s political differences end at the water’s edge,” he said after the G-20 summit in Los Cabos, Mexico.

But standing on the world stage Thursday, Obama showed no hesi­ta­tion to attack Trump. Fredrik Logevall, the Laurence D. Belfer professor of international affairs at Harvard University, said in an email that outgoing presidents “typically maintain a low profile in these situations. They seek to remain above the fray — it’s more dignified, more presidential.

“I can’t think of a prior post-World War II case in which a sitting president has questioned publicly and openly the fitness for office of a major party nominee,” Logevall added.

For months, Obama operated on the assumption that Trump would simply not become president. During a trip to Asia in February, he said Americans are too “sensible” to elect him. In April, he recounted how he had reassured one of his wealthy Hollywood donors, “Mr. Trump is not succeeding me.”

But as polls show a dead heat between Trump and Hillary Clinton, and concerns about her use of a private email server at the State Department persist, the president has become increasingly vocal about the prospect of Trump taking office.

To some extent, the entire concept of political feuds ending at U.S. shores is “an aphorism that serves to shore up an apparent consensus while quieting dissenters,” said Nicole Hemmer, a visiting research associate at the University of Virginia’s Miller Center. What started as a pledge to forge a united front during the Cold War with the Soviet Union showed cracks even during that period and has become even more visible on foreign trips in recent years.

Obama criticized Bush’s foreign policy during a visit to Germany during his 2008 presidential bid. Romney repaid the favor to Obama during a trip to Europe and Israel four years later. But Trump’s political rise has upended the traditional calculations both in Washington and world capitals across the globe. The Republican has suggested that Japan, South Korea and Saudi Arabia should consider developing nuclear weapons, because proliferation is “going to happen anyway.”

He argues for erecting a wall on the southern border to keep undocumented Mexican immigrants out of the United States and that the United States should have a “total and complete” ban on Muslims ” until our country’s representatives can figure out what is doing on.”

British Prime Minister David Cameron called the proposed Muslim ban “stupid and wrong,” while Sadiq Khan, the mayor of London, said Trump is “ignorant” about Islam. Trump, for his part, called Khan’s comments “very nasty” and told ITV host Piers Morgan to convey a message to Cameron: “Tell him I will remember those statements.”

[Two former Mexican presidents take aim at Trump]

Former Mexican president Vicente Fox has used an expletive to describe Trump’s wall-building scheme, and even Pope Francis referred to it in scathing terms by telling journalists aboard his papal plane that “a person who thinks only about building walls . . . is not a Christian.”

But Trump has shot back at them, too, and the man who had just secured enough delegate commitments to claim the GOP nomination had a warning for Obama, as well.

“He is a man who shouldn’t be really, you know, airing his difficulties, and he shouldn’t be airing what he’s airing where he is right now,” Trump said. “And I think that you’re going to see it stop pretty soon.”

Jenna Johnson in Bismarck, N.D., contributed to this report.