Prevent switching workspaces when clicking on dock icon

Solution 1:

Open Terminal.app, then run the following commands.

Change the default workspace preference:

defaults write com.apple.dock workspaces-auto-swoosh -bool NO

Next, restart the Dock with this command:

killall Dock

References:
How can I disable auto-switching desktops?
Disable Space switching on Command-Tab

Solution 2:

Unlike the original questioner, I was trying to enable this effect. I wanted to switch between spaces where applications lived on different desktops.

I just took a guess at:

defaults write com.apple.dock workspaces-auto-swoosh -bool TRUE

and

killall Dock

The two commands above worked like a charm to restore the effect.