Maybe they are legal (definitely not here in California) but the moral argument you’re making doesn’t make much sense because it works both ways - company has no intention of unlearning your past years of experience when you leave and so neither must you