문제

In Ruby we can add a new method to a previously defined class by dynamically modifying it at runtime:

class String
  def to_magic
    "magic"
  end
end

Is it possible to do the same in JavaScript? If yes, how?

도움이 되었습니까?

해결책

The equivalent in JavaScript:

String.prototype.toMagic = function(){ 
   return "magic"; 
}

console.log("".toMagic());//>>> "magic"
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top