There two ways to define a "jQuery plugin" in Scala.js. The first one is a safe and idiomatic Scala-esque way, but is only for consumption by other Scala.js code. The second is a bit ugly, but it can be used by JavaScript code.
For Scala.js
In Scala, and in Scala.js, the patch-the-prototype thing that actual jQuery plugins do is discouraged. Instead, we use implicit classes:
implicit class JQueryGreenify(val self: JQuery) extends AnyVal {
def greenify(): self.type = {
self.css("color", "green")
self
}
}
And then you can simply do
jQuery("#some-element").greenify()
when you have JQueryGreenify
in scope (as an import, typically).
This method does not pollute the actual prototype of jQuery objects. It is a pure-Scala abstraction. It is clean and safe, but that means it cannot be used by JavaScript code.
For JavaScript
If you need to call greenify
from JavaScript, you actually need to add a function property on the jQuery.fn
object. Since the function needs to use the this
parameter, we have to explicitly ascribe it to a js.ThisFunction
(see also Access the JS this from scala.js):
js.Dynamic.global.jQuery.fn.greenify = { (self: JQuery) =>
self.css("color", "green")
self
}: js.ThisFunction
which is basically a transliteration of the original JavaScript code from the question.