Вопрос

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