Skip to content

Latest commit

 

History

History
21 lines (19 loc) · 493 Bytes

function15.md

File metadata and controls

21 lines (19 loc) · 493 Bytes

避免类型检查(下)

Bad logo

	function combine(val1, val2) {
	  if (
	    (typeof val1 === "number" && typeof val2 === "number") ||
	    (typeof val1 === "string" && typeof val2 === "string")
	  ) {
	    return val1 + val2;
	  }
	
	  throw new Error("Must be of type String or Number");
	}

Good logo

	function combine(val1, val2) {
	  return val1 + val2;
	}